简体   繁体   中英

force json from jersey response

I am trying to implement this example . The first method in FileResource is supposed to return a json response, but the response is coming through as XML. I have never used jersey before. I tried adding @produces yielding this:

@GET @Produces("application/json")
@Path("/url")
public Response getCallbackUrl() {
    String url = blobstoreService.createUploadUrl("/rest/file");
    return Response.ok(new FileUrl(url)).build();
}

Now I'm getting

A message body writer for Java class FileUrl, and Java type class FileUrl, and MIME media type application/json was not found

How can I fix this? Thanks for any help. And if you need any more info let me know. I don't know much of the jersey terminology.

You need a few more dependencies in order to produce JSON output.

Take a look at: http://jersey.java.net/nonav/documentation/latest/chapter_deps.html

Scroll down to:

11.4.1.2. MOXy Maven developers, using JSON serialization support of JAXB beans when using the MIME media type application/json require a dependency on the jersey-json module (explicit dependency on org.eclipse.persistence.moxy is required).


An alternative to "vanilla" Jersey that you might be interested in is Dropwizard . Their blurb is:

Dropwizard has out-of-the-box support for sophisticated configuration, application metrics, logging, operational tools, and much more, allowing you and your team to ship a production-quality HTTP+JSON web service in the shortest time possible.

Yep you'll need some jersey / json dependency here :

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.2</version>
</dependency>

with your version of jersey of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM