For Spring Boot 1.2.3, how to set ignore null value in JSON serialization?

In Spring Boot 1.2.3, we can configure Jackson ObjectMapper through the properties file. But I did not find an attribute that could set Jackson to ignore a null value when serializing the Object to JSON string.

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

I want to archive the same code as

 ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL); 
+7
spring jackson spring-boot
source share
5 answers

This was an improvement for Spring Boot 1.3.0.

So, unfortunately, you will need to configure it programmatically to 1.2.3

 @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) public class Shop { //... } 
+3
source share

Add the following line to your application.properties file.

spring.jackson.default-property-inclusion = non_null

For Jackson versions prior to version 2.7:

spring.jackson.serialization-inclusion = non_null

+23
source share

For Spring Boot 1.4.x, you can include the following line in your application.properties

spring.jackson.default-property-inclusion=non_null

+6
source share

This was a good solution to deprecation: @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

But now you should use:

@JsonInclude(JsonInclude.Include.NON_NULL) public class ClassName { ...

You can look here: https://fasterxml.imtqy.com/jackson-annotations/javadoc/2.7/com/fasterxml/jackson/annotation/JsonInclude.Include.html

+2
source share

Class wide

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

Property Width:

 public class MyModel { ..... @JsonInclude(JsonInclude.Include.NON_NULL) private String myProperty; ..... } 
0
source share

All Articles