I am using jersey to create a REST API for a service. I would like to be able to accept and return both JSON and XML, and basically it works, but I don't like the standard “mapped” JSON flavor that Jersey likes to spit out.
I know of a newer “natural” notation (from http://jersey.java.net/nonav/documentation/latest/json.html , which I will quote in detail, because this makes problems with the standard “displayed” notation obvious):
After using the matched JSON notation for some time, it was obvious that you need to configure all the different things manually, which can be a bit problematic. To avoid manual work, a new, natural, JSON notation was introduced in version 1.0.2 of Jersey. With natural notation, Jersey will automatically determine how individual items should be handled, so you don't need to do any configuration instructions. Java arrays and lists are mapped to JSON arrays, even for singleton cases. Java numbers and booleans are correctly mapped to JSON numbers and booleans, and you don’t have to bother with XML attributes like in JSON, they retain the original names
and would like to use it everywhere, but I could not figure out how to do it. I create / configure Jersey through Tomcat XML configuration files - using what I consider to be a regular dance with servlet / servlet-class / init-param tags, but I could not find the documentation about how to specify JSONConfiguration options.
I also tried to implement my own ContextResolver, which uses JSONJAXBContext I, created from Java code, where I can apply JSONConfiguration.natural () (an example of this looks like this answer ). This works, but only for types that I explicitly list in this code, and go to the JSONJAXBContext constructor. This extra code is not only written and maintained, but also changed if I add more data classes, but it doesn’t work for things like List.
Is there a way to tell Jersey to just use natural notation instead of the displayed notation, always and for all types?