我有一个类需要使用jackson进行反序列化,并且该类具有集合属性.该集合为空,但不为null.问题:如何在没有空集合的情况下对类进行desersialise .示例代码如下:
class Person
{
String name;
List<Event> events;
//....getter, setter
}
Run Code Online (Sandbox Code Playgroud)
如果
person.list = new List<Event>();
persion.name = "hello";
Run Code Online (Sandbox Code Playgroud)
然后除了json将是:
{name: "hello"}
Run Code Online (Sandbox Code Playgroud)
不
{name: "hello", events:[]}
Run Code Online (Sandbox Code Playgroud)
怎么做?谢谢~~
================================================
我已经通过n1ckolas的建议解决了这个问题.先谢谢你 我的杰克逊版本是2.1.1,而Spring-3.2.2导入了对杰克逊这个版本的更好支持.此外,这适用于数组和集合. 以下是我的配置:
<!-- 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>
Run Code Online (Sandbox Code Playgroud)