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.

About

manveen

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today