Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I customize serialization of a list of JAXB objects to JSON?
    primarykey
    data
    text
    <p>I'm using Jersey to create a REST web service for a server component.</p> <p>The JAXB-annotated object I want to serialize in a list looks like this:</p> <pre><code>@XmlRootElement(name = "distribution") @XmlType(name = "tDistribution", propOrder = { "id", "name" }) public class XMLDistribution { private String id; private String name; // no-args constructor, getters, setters, etc } </code></pre> <p>I have a REST resource to retrieve one distribution which looks like this:</p> <pre><code>@Path("/distribution/{id: [1-9][0-9]*}") public class RESTDistribution { @GET @Produces("application/json") public XMLDistribution retrieve(@PathParam("id") String id) { return retrieveDistribution(Long.parseLong(id)); } // business logic (retrieveDistribution(long)) } </code></pre> <p>I also have a REST resource to retrieve a list of all distributions, which looks like this:</p> <pre><code>@Path("/distributions") public class RESTDistributions { @GET @Produces("application/json") public List&lt;XMLDistribution&gt; retrieveAll() { return retrieveDistributions(); } // business logic (retrieveDistributions()) } </code></pre> <p>I use a ContextResolver to customize JAXB serialization, which is currently configured like this:</p> <pre><code>@Provider @Produces("application/json") public class JAXBJSONContextResolver implements ContextResolver&lt;JAXBContext&gt; { private JAXBContext context; public JAXBJSONContextResolver() throws Exception { JSONConfiguration.MappedBuilder b = JSONConfiguration.mapped(); b.nonStrings("id"); b.rootUnwrapping(true); b.arrays("distribution"); context = new JSONJAXBContext(b.build(), XMLDistribution.class); } @Override public JAXBContext getContext(Class&lt;?&gt; objectType) { return context; } } </code></pre> <p>Both REST resources work, as well as the context resolver. This is an example of output for the first one:</p> <pre><code>// path: /distribution/1 {"id":1,"name":"Example Distribution"} </code></pre> <p>Which is exactly what I want. This is an example of output for the list:</p> <pre><code>// path: /distributions {"distribution":[{"id":1,"name":"Sample Distribution 1"},{"id":2,"name":"Sample Distribution 2"}]} </code></pre> <p>Which is not quite what I want.</p> <p>I don't understand why there is an enclosing <code>distribution</code> tag there. I wanted to remove it with <code>.rootUnwrapping(true)</code> in the context resolver, but apparently that only removes another enclosing tag. This is the output with <code>.rootUnwrapping(false)</code>:</p> <pre><code>// path: /distribution/1 {"distribution":{"id":1,"name":"Example Distribution"}} // not ok // path: /distributions {"xMLDistributions":{"distribution":[{"id":1,"name":"Sample Distribution 1"},{"id":2,"name":"Sample Distribution 2"}]}} </code></pre> <p>I also had to configure <code>.arrays("distribution")</code> to always get a JSON array, even with only one element.</p> <p>Ideally, I'd like to have this as an output:</p> <pre><code>// path: /distribution/1 {"id":1,"name":"Example Distribution"} // currently works // path: /distributions [{"id":1,"name":"Sample Distribution 1"},{"id":2,"name":"Sample Distribution 2"}] </code></pre> <p>I tried to return a <code>List&lt;XMLDistribution&gt;</code>, a <code>XMLDistributionList</code> (wrapper around a list), a <code>XMLDistribution[]</code>, but I couldn't find a way to get a simple JSON array of distributions in my required format.</p> <p>I also tried the other notations returned by <code>JSONConfiguration.natural()</code>, <code>JSONConfiguration.mappedJettison()</code>, etc, and couldn't get anything resembling what I need.</p> <p>Does anyone know if it is possible to configure JAXB to do this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload