Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to configure JacksonJaxbJsonProvider in a single line of code?
    primarykey
    data
    text
    <p>I use Jackson for JSON serialisation in a REST application like this:</p> <pre><code>import javax.ws.rs.core.Application; import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider; import org.codehaus.jackson.map.SerializationConfig; public class MyApplication extends Application { public Set&lt;Class&lt;?&gt;&gt; getClasses() { Set&lt;Class&lt;?&gt;&gt; classes = new HashSet&lt;Class&lt;?&gt;&gt;(); classes.add(MyResource1.class); classes.add(MyResource2.class); ... return classes; } public Set&lt;Object&gt; getSingletons() { Set&lt;Object&gt; singletons = new HashSet&lt;Object&gt;(); singletons.add(new JacksonJaxbJsonProvider().configure( SerializationConfig.Feature.WRITE_NULL_PROPERTIES, false)); // (*) return singletons; } } </code></pre> <p>(*) Necessary otherwise fields annotated with @XmlElementRef will appear in JSON output as <code>"myField": null</code> when null (instead of not appearing), while it's fine for fields annotated with @XmlElement.</p> <p>However the use of <code>SerializationConfig.Feature.WRITE_NULL_PROPERTIES</code> is deprecated in favor of <code>SerializationConfig.setSerializationInclusion(..)</code> (1) which itself is deprecated in favor of <code>SerializationConfig.withSerializationInclusion(..)</code> or <em>configure through ObjectMapper</em> (2)</p> <p>But what I think is done using <code>new JacksonJaxbJsonProvider().configure(..)</code> is indeed configuring an ObjectMapper (I took a look at the code).</p> <p>So my question is: how do I configure this properly without using any deprecated property and without (re)creating new config class(es)?</p> <p>(1) <a href="http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/SerializationConfig.Feature.html#WRITE_NULL_PROPERTIES" rel="nofollow noreferrer">http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/SerializationConfig.Feature.html#WRITE_NULL_PROPERTIES</a></p> <p>(2) <a href="http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/SerializationConfig.html#setSerializationInclusion(org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion)" rel="nofollow noreferrer">http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/SerializationConfig.html#setSerializationInclusion(org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion)</a></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.
    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