2015-05-05 44 views
7

在Spring Boot 1.2.3中,我们可以通过属性文件定制Jackson ObjectMapper。但是我没有发现一个属性可以设置杰克逊在将对象序列化为JSON字符串时忽略空值。对于Spring Boot 1.2.3,如何在JSON序列化中设置忽略空值?

spring.jackson.deserialization.*= # see Jackson's DeserializationFeature 
spring.jackson.generator.*= # see Jackson's JsonGenerator.Feature 
spring.jackson.mapper.*= # see Jackson's MapperFeature 
spring.jackson.parser.*= # see Jackson's JsonParser.Feature 
spring.jackson.serialization.*= 

我要存档像

ObjectMapper mapper = new ObjectMapper(); 
mapper.setSerializationInclusion(Include.NON_NULL); 

回答

23

编程配置它下面的行添加到您的application.properties文件。

spring.jackson.default属性模型 - 包裹体= non_null

对于之前2.7杰克逊的版本:

spring.jackson.serialization系夹杂物= non_null

+0

这只适用于Spring Boot版本1.3.0 – Smiderle

+0

请记住不要使用 新的RestTemplate() 因为它不会使用此配置,而是创建默认转换器。 RestTemplateBuilder.build() 使用所有配置 –

6

对于Spring Boot 1.4.x,您可以将以下行添加到您的application.properties

spring.jackson.default-property-inclusion=non_null

0

类范围,

@JsonInclude(JsonInclude.Include.NON_NULL) 
public class MyModel { .... } 

物业范围:

public class MyModel { 
    ..... 

    @JsonInclude(JsonInclude.Include.NON_NULL) 
    private String myProperty; 

    ..... 
} 
相关问题