Jackson with jaxb

when using the Jackson JSON processor in Jersey, when and why do I need to use JAXB annotations between them? Object->JAXB->JSON

Jackson also provides its own JAX-RS provider direct Object->JSON . what is missing in this approach? or why do I prefer more than

ps: I also use spring

+3
java jackson jersey jax-ws jaxb
May 14 '11 at 12:28
source share
2 answers

To create JSON, you usually need to specify @Produces(MediaType.APPLICATION_JSON) . However, by default, the JAXB route will be completed.

With an object -> JAXB -> JSON, you will need to annotate the classes you want to display using @XmlRootElement . This will work fine, but once you get the HashMap serialized, you won’t get the obvious {keyOne:"one",keyTwo:"two"} , but rather something weird like {entry:[{key:"keyOne",value:"one"},{key:"keyTwo",value:"two"}]} .

So, to take a direct object -> JSON, simply specify the following in your web.xml:

  <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> 

With this mapping, JSON will work the way you expected it to work. Just remember to remove the @XmlRootElement annotations, they force XML to be generated when POJO mapping is enabled.

Also consider my question regarding this: Java.util.Map to JSON object with Jersey / JAXB / Jackson

Link: http://jersey.java.net/nonav/documentation/latest/json.html#d4e894

+10
May 14 '11 at 14:36
source share

You only need to use JAXB annotations if you also want to create / consume XML data.

If you just care about JSON, don't use JAXB annotations; there is nothing that they offer beyond Jackson's annotations. And in fact, most basic cases can be handled without any annotations using the Java Bean naming conventions.

+2
May 15 '11 at 14:32
source share



All Articles