• REST
    February 22, 2008

Better JSON Available In Jersey

Since today a better support for JSON data generated out of JAXB beans is available in Jersey.

The main improvement is a simpler default JSON data format.
Badgerfish convention was replaced by a new one, a slightly simplified so called mapped convention from Jettison. JSON convention used for particular beans is now also fully configurable by the end user.
I will show how the configuration could be done below.

The main differences between available formats could be described using the following example.
Lets start with a very simple JAXB bean. You can see the source code, XML output and then
all formats you can get from Jersey resource:

public static class SimpleJaxbBean {
public String name = "Franz";
public String surname = "Kafka";

XML output:


Badgerfish format:


Mapped format (Jettison):


and finally Jersey default format:


If you want to control, which format is actually used for which JAXB bean,
you can do it by providing Jersey runtime with your own custom JAXBContext classes
via implementing custom ContextResolver.
You can get inspired by following example:

public class SimpleJsonResource {
@Providerpublic static class JAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class[] types = {SimpleJaxbBean.class};public JAXBContextResolver() throws Exception {
Map<String, Object> props = new HashMap<String, Object>();
this.context = new JSONJAXBContext(types, props);
}public JAXBContext getContext(Class<?> objectType) {
return (types[0].equals(objectType)) ? context : null;

public static class SimpleJaxbBean {
public String name = "Franz";
public String surname = "Kafka";
@ProduceMime({"application/json", "application/xml"})
public SimpleJaxbBean getJson() {
return new SimpleJaxbBean();

The following options are available for the JSONJAXBContext.JSON_NOTATION
property, where the first one is the default value:


In the future i plan to add more properties so that the JSON serialization and deserialization
is highly configurable.

Join the discussion

Comments ( 19 )
  • Eduardo Pelegri-Llopart Friday, February 22, 2008


    What about showing how to install this on top of the GFv3 M2 that was announced today?

    - eduard/o

  • Eric Jensen Friday, February 29, 2008

    This is definitely a welcome change over badgerfish, but it's proving confusing to me for things that return lists. The default behavior is to take a JAXB bean that would be serialized as:











    and turn it into


    which is clearly not what i want...help?

  • Jakub Monday, March 3, 2008

    Hi Eric. Thanks for pointing that out. It should be something like: {"item":[{"k":"1", "v":"A"},{"k":"2","v":"B"}]}, right? I am going to create a test case for this and fix it.

  • Jakub Monday, March 3, 2008

    Eric, i responded too fast. The above mentioned JSON gets produced just fine.

    Please see the unit test at https://jersey.dev.java.net/source/browse/jersey/trunk/jersey/test/com/sun/ws/rest/impl/json/ for a similar case.

    Could you please give an example of desired JSON expression you want to get produced?



  • Julien Saturday, July 5, 2008

    How to control the marshalling process when i create a web method which produces XML and json ouputs from an object serialization ? In my case the object is annoted with Jaxb annotations like (@XmlAttribute or @XmlList) and i want to export this object to a custom XML and a simply json ({ id:1, name:"name", categories:[{id:1, name:"name"}]} and not ({@id:1 $:"name"}). In fact i want to bypass the jaxb customization in some cases :-)

  • Jakub Monday, July 7, 2008

    Julien, it seems you are giving me another reason to make internal JAXB model information accessible to outside. Please stay tuned. I will talk to Martin Grebac, who actually owns JAXB and let you know (he is off on vacation this week).

  • Tim Wednesday, July 23, 2008

    I just ran into the same problem as Julien, so I thought I'd ping here to see if there was any update on this yet. I just need to get rid of the "@" prefix on attribute names. Even if there's not an elegant way to do this yet, I'd settle for a hacky way for now. :)

  • Jakub Friday, July 25, 2008

    I am sorry, but the only thing i can promise at the moment is that i will hopefully have time for it after 0.9 release in August. I would indeed love if i could write some better answer.

  • Jakub Friday, September 12, 2008

    Tim: the initial support for serialization of attributes without the initial '@' in the name has been just commited to the main trunk. It should soon become available at


    The way to make it work for you is to provide an additional property to your custom JAXBContext resolver with a list of affected attribute names. E.g. for attributes i, j you would add:

    props.put(JSONJAXBContext.JSON_ATTRS_AS_ELEMS, "[\\"i\\",\\"j\\"]");

    so that it gets serialized out as


    instead of


  • Tim Friday, September 12, 2008

    Awesome, thanks Jakub! Is there any way to enable this for all elements (like "\*")?

  • Jakub Monday, September 15, 2008

    Not yet. The thing is that i need to better integrate with JAXB. And it might require certain changes on JAXB side. The plan is to look at that after Jersey 1.0 is released.

  • Clark Tuesday, November 18, 2008

    Just wondering about how to specify a ContextResolver based on MimeType. I'm returning both XML and JSON using JAXB and I need to customize them both (arrays, as above for JSON and some namespace issues for XML). How can I specify a resolver for JSON vs XML, since they both would be JAXBContext Providers? Thanks.

  • Jakub Wednesday, November 19, 2008

    Clark: JSONJAXBContext is just a wrapper. Besides JSON specific properties, you can pass it also properties for the underlying JAXBContext, and it should work all right. If you need a very special JAXBContext implementation, just override the standard one, and it should work as well (wrapped instance is instantiated via standard JAXB API JAXBContext.newInstance factory method, so your implementation should be picked up automatically).

  • xmariachi Thursday, June 11, 2009


    something I'd like to do, which I cannot find, is how to set a default notation (natural in my case) throughout the whole application.

    Is there a way to set a notation configuration for either whole application or all classes?

    I've seen many examples where this is linked to the class - but that forces you to specify the class on the code. Every time you have a new class you want to notate, then you have to specify it on the Class[] types.

    Thanks a lot

  • Ashitkin Alexander Monday, October 19, 2009

    I was looking for unwrapped json and found it at last. By-by DWR! I'm happy :)

  • links of london Wednesday, November 25, 2009

    Great post and draw. Thank you for sharing.!!@!

  • uvip Thursday, December 17, 2009

    That’s something,That's what I was thinking.Brilliant idea.

  • Kareem Friday, January 1, 2010

    I have sub-classed the ContextResolver<JAXBContext> so that JSON NATURAL format is used with the classes I need to bind. However, one of these classes uses XML attributes and still the JSON strings produced contain the '@' prefix for attributes. I thought that sub-classing would automatically register the new provider with Jersey. But it seems that the new provider is not registered. Could you please help me with that?

  • burton james furniture Thursday, January 21, 2010

    I have to agree and I will come back to follow you more.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.