How can I convert Java object to Json in spray routing
<p>I would like to know if there is any way to return Java object as Json String inside spray routing.</p>

<p>For now, I am converting my Java object (which is returned my UserActor) into Json using Jackson through a util class(JacksonUtil) created by me.</p>

<p>Here is the code snippet for reference : </p>

<pre><code>val route = {
    path("users" / Segment) { id =&gt;
      parameterMap { params =&gt;
        get {
          respondWithMediaType(MediaTypes.`text/plain`) {
            val resource: GetResource = new GetResource
            resource.searchCriteriaList.add(new SearchCriteria("userId", "=", Segment)) 
            logger.info("Request Received in new route: " + resource)
            onComplete(callUserActor(resource)) {
              case Success(ret: User) =&gt; {
                logger.info("Request complete. Success.")
                complete(JacksonUtil.toJson(ret))
              }
              case Failure(error) =&gt; {
                logger.info("Request complete. Failure. " + error)
                complete("Error: " + error)
              }
            }
          }
        }
      }
    } 
  }

def callUserActor(getResource: GetResource): Future[User] = {
    (userActor ? getResource).mapTo[User]
  }
</code></pre>

<p>GetResource is the class which take of query parameters.
For example if client hits GET /users/123, then GetResource will add a searchcriteria as userId = "123"</p>