For Spring Boot 1.2.3, how to set ignore null value in JSON serialization?
Solution 1
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 {
//...
}
Solution 2
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
Solution 3
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
Solution 4
For Spring Boot 1.4.x, you can include the following line to your application.properties
spring.jackson.default-property-inclusion=non_null
Solution 5
Class-wide,
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MyModel { .... }
Property-wide:
public class MyModel {
.....
@JsonInclude(JsonInclude.Include.NON_NULL)
private String myProperty;
.....
}
Related videos on Youtube
crisli
Updated on August 02, 2022Comments
-
crisli almost 2 years
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 the Object to JSON string.
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.*=
I want to archive the same code like
ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL);
-
crisli almost 9 yearsThanks a lot, very helpful.
-
Smiderle almost 8 yearsthis only available for Spring Boot version 1.3.0
-
Svitlana Onish over 6 yearsremember not to use new RestTemplate() because it would not use this config,but create default converters. RestTemplateBuilder.build() uses all configs
-
user674669 about 6 yearsLooks like the enum com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion was deprecated in ver 2.6 of jackson-databind
-
Ravat Tailor almost 6 years@cjungel, I have tried this solution but not working for me, I am using 1.5.7 version of spring boot
-
Abhidemon almost 6 yearsI am using
1.5.10.RELEASE
version and it worked like charm for me. -
Vinay Prajapati over 5 yearsit says Inclusion is deprecated
-
Aarish Ramesh over 5 yearsThis inclusion is deprecated. Instead @JsonInclude(JsonInclude.Include.NON_NULL) needs to be used as stated in the below answer
-
WesternGun over 5 yearsCheck the complete list in: docs.spring.io/spring-boot/docs/current/reference/html/…
-
mliu almost 3 yearsThis was the right solution for me, as I didn't want to add annotations to my shared object classes