Kin*_*rsi 20 java json jackson
我有一个类需要使用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)
Eyl*_*lon 37
您可以在类或文件上使用@JsonInclude(JsonInclude.Include.NON_EMPTY)注释.
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
class Person
{
String name;
List<Event> events;
//....getter, setter
}
Run Code Online (Sandbox Code Playgroud)
如果您可以使用Jackson注释更改原始模型,以下是实现此目的的方法.
杰克逊有WRITE_EMPTY_JSON_ARRAYS.你可以用以下方式关闭它:
objectMapper.configure(SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS, false);
Run Code Online (Sandbox Code Playgroud)
但它只适用于Arrays
,但不适用于Collections
.但是你可以结合@JsonProperty
使用@JsonIgnore
,如下所示:
//....getter, setter of Person class
@JsonIgnore
public List<Event> getEvents() {
return events;
}
@JsonProperty("events")
public Event[] getEventsArr() {
return events.toArray(new Event[0]);
}
Run Code Online (Sandbox Code Playgroud)
之后你会有你想象的输出.
编辑:如果您使用的是SpringMVC,那么您可以在以下方面配置您的实际ObjectMapper
参考mvc:annotation-driven
:
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="objectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--custom Json Mapper configuration-->
<bean name="objectMapper" class="org.springframework.http.converter.json.JacksonObjectMapperFactoryBean" autowire="no">
<property name="featuresToDisable">
<list>
<!--Ignore unknown properties-->
<value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_EMPTY_JSON_ARRAYS</value>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
Offtop:通常指定显式实例非常有用ObjectMapper
,因为:
@Autowired
如果您使用的是 Spring Boot,只需添加application.properties
以下内容:
spring.jackson.serialization-inclusion=NON_EMPTY
Run Code Online (Sandbox Code Playgroud)
使用这个注解 @JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
person.list = new List<Event>();
Run Code Online (Sandbox Code Playgroud)
来自文档
指示仅不包含具有空值或被视为空的属性的值。
归档时间: |
|
查看次数: |
32585 次 |
最近记录: |