34
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 without empty collection. Sample code below:
class Person
{
String name;
List<Event> events;
//....getter, setter
}
if
person.list = new List<Event>();
persion.name = "hello";
then excepted the json would be:
{name: "hello"}
not
{name: "hello", events:[]}
How to make it? Thank you~~
================================================
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 arrays and collection. Below is my configuraion:
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--Json Mapper-->
<bean name="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" autowire="no">
<property name="featuresToDisable">
<list>
<!--not to return empty colletion-->
<value type="com.fasterxml.jackson.databind.SerializationFeature">WRITE_EMPTY_JSON_ARRAYS</value>
</list>
</property>
</bean>