The road less taken

« An introduction to... | Main | Pattern to implement... »
Thursday Feb 14, 2008

Content negotiation in Jersey

Jersey serializes JAXB beans as JSON (BadgerFish convention) or XML.

Lets say I have an implementation of my service as below,

@UriTemplate("/myresource/")
public class MyResource {
   @HttpMethod
   @ProduceMime({"application/xml", "application/json"})
   public JAXBBean get() {
      JAXBBean j = ...
      return j;
   }
}

What do you have to do in your client to get the result in JSON format?

Jersey uses HTTP headers to determine value returned: If there is no accept header or the accept is "*", "application/*" or "application/xml" then the JAXB bean will be serialized as XML. If the accept header is "application/json" then the JAXB bean will be serialized as JSON. Content negotiation is done solely through the HTTP Accept header.

Another tip?
For testing from a browser try the Firefox add-on Poster. It let's you specify the HTTP method, set HTTP request headers and send arbitrary content in the body of requests.

Comments:

Note Jersey currently uses of the Badger Fish convention and we are very aware of its deficiencies . We are working on improving this to support much more consumer friendly JSON in conjunction with JAXB beans e.g. so things can work with jMaki.

Posted by Paul Sandoz on February 15, 2008 at 09:33 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed