Can I force Jersey to use the natural JSON notation globally / by default?

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?

+4
source share
1 answer

I did not find the answer to the actual question I asked here, but instead I found a simple three-step process that performs the same end result that I wanted:

  • add jackson to my project
  • configure jersey to enable FEATURE_POJO_MAPPING
  • I hit myself on the head several times because it turned out to be so simple.

The Jersey documentation mentions this POJOMappingFeature / FEATURE_POJO_MAPPING noticeably (this is the first example on the page of the document I linked in the question), but does not describe exactly what this means, and from the way this document presents its information, I thought it was an option ( 5.1, “POJO Support”) did not match option 5.2 (“JAXB-based JSON Support”), which was more like what I wanted. So I tried many other things before trying to enable FEATURE_POJO_MAPPING.

But as soon as I tried, it worked exactly the way I wanted, and I did not have to look back.

The advantage of this is that Jackson generates much better error messages if the client passes its fake JSON content, compared to the implementation of Jersey-based JSON.

+5
source

Source: https://habr.com/ru/post/1392840/


All Articles