In the Spring Boot 1.2.3, we can customize the Jackson ObjectMapper via properties file. But I didn\'t find a attribute can set Jackson ignore null value when serialization
Class-wide,
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MyModel { .... }
Property-wide:
public class MyModel {
.....
@JsonInclude(JsonInclude.Include.NON_NULL)
private String myProperty;
.....
}
Add the following line to your application.properties
file.
spring.jackson.default-property-inclusion=non_null
For versions of Jackson prior to 2.7:
spring.jackson.serialization-inclusion=non_null
This was a good solution before deprecation:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
But now you should use:
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ClassName {
...
You can take a look here: https://fasterxml.github.io/jackson-annotations/javadoc/2.7/com/fasterxml/jackson/annotation/JsonInclude.Include.html
For Spring Boot 1.4.x, you can include the following line to your application.properties
spring.jackson.default-property-inclusion=non_null
@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder mapperBuilder) {
return mapperBuilder.build().setSerializationInclusion(Include.NON_NULL);
}
that works for me
This was an enhancement for Spring Boot 1.3.0.
So unfortunately you'll need to configure it programmatically on 1.2.3
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Shop {
//...
}