Jersey JSON sort empty lists

We have a list of Java objects that is sorted through the JSON that Jersey created.

The list is called "strings." When there is data, we have:

{"records":"1","page":"1","total":"1","rows":[{"id":"692334","cell":["ECS","D","201009","","0","ABCD","11","11","","201009"]}]} 

If no data

 {"page":0,"records":0,"total":0} 

How can we include jersey in the rows field, even if List is 0? We want:

 {"page":0,"records":0,"total":0,"rows":[]} 

Note that we are already using the JAXB ContextResolver to make sure that JSON is true on a single line. Not sure if we can configure this converter to solve our problem.

+4
source share
3 answers

Use the Jackson JAX-RS provider instead of alternatives (badgerfish / jettison) that does not perform XML-JSON conversion. The missing array is probably related to this conversion. There are several ways to configure this (the mailing list for jerseys should have several), and the latest versions can be submitted directly through the Jersey API.

+5
source

Maybe this will help you: http://jersey.java.net/nonav/documentation/latest/json.html#d4e903

It seems that some problems with arrays can be solved using something like this:

 JSONConfiguration.mapped().arrays("yourArrayName").build() 

At least it solves the problem when the list contains only 1 element, which is also formed as a JSON array.

0
source

I was able to solve an array of JSON errors in json jersey library. The secret ingredient is mentioned earlier. JSONConfiguration and ContextResolver magic. See My next post, it has a complete code sample, custom ContextResolver and the rest. An application class may be somewhat fuzzy at first glance.

How to serialize Java primitives with Jersey REST

  • json array for Java null or singleton lists
  • primitive integer or logical fields without quotes
0
source

All Articles