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
samy-delux May 14 '11 at 14:36 2011-05-14 14:36
source share