Note that there are some explanatory texts on larger screens.

plurals
  1. PODon't return property when it's an empty list with jackson
    primarykey
    data
    text
    <p>I have a class need to be dessrialized using jackson and the class has an collection property. The collection is empty, but not null. Question: How to desersialise the class <b>without</b> empty collection. Sample code below:</p> <pre><code>class Person { String name; List&lt;Event&gt; events; //....getter, setter } </code></pre> <p>if </p> <pre><code>person.list = new List&lt;Event&gt;(); persion.name = "hello"; </code></pre> <p>then excepted the json would be:</p> <pre><code>{name: "hello"} </code></pre> <p>not </p> <pre><code>{name: "hello", events:[]} </code></pre> <p>How to make it? Thank you~~</p> <p>================================================</p> <p>I have solved this by n1ckolas's advice. Thank you first. My jackson version is 2.1.1, and Spring-3.2.2 import better support for this verson of jackson. Also, this works for both <B> arrays and collection.</B> Below is my configuraion:</p> <pre><code>&lt;!-- Enables the Spring MVC @Controller programming model --&gt; &lt;mvc:annotation-driven&gt; &lt;mvc:message-converters&gt; &lt;bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt; &lt;property name="objectMapper" ref="objectMapper"/&gt; &lt;/bean&gt; &lt;/mvc:message-converters&gt; &lt;/mvc:annotation-driven&gt; &lt;!--Json Mapper--&gt; &lt;bean name="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" autowire="no"&gt; &lt;property name="featuresToDisable"&gt; &lt;list&gt; &lt;!--not to return empty colletion--&gt; &lt;value type="com.fasterxml.jackson.databind.SerializationFeature"&gt;WRITE_EMPTY_JSON_ARRAYS&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre>
    singulars
    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.
 

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