2015-05-05 47 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; 

    ..... 
} 
相關問題