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

51,636

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;

    .....
}
Share:
51,636

Related videos on Youtube

crisli
Author by

crisli

Updated on August 02, 2022

Comments

  • crisli
    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
    crisli almost 9 years
    Thanks a lot, very helpful.
  • Smiderle
    Smiderle almost 8 years
    this only available for Spring Boot version 1.3.0
  • Svitlana  Onish
    Svitlana Onish over 6 years
    remember not to use new RestTemplate() because it would not use this config,but create default converters. RestTemplateBuilder.build() uses all configs
  • user674669
    user674669 about 6 years
    Looks like the enum com.fasterxml.jackson.databind.annotation.JsonSerialize.Incl‌​usion was deprecated in ver 2.6 of jackson-databind
  • Ravat Tailor
    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
    Abhidemon almost 6 years
    I am using 1.5.10.RELEASE version and it worked like charm for me.
  • Vinay Prajapati
    Vinay Prajapati over 5 years
    it says Inclusion is deprecated
  • Aarish Ramesh
    Aarish Ramesh over 5 years
    This inclusion is deprecated. Instead @JsonInclude(JsonInclude.Include.NON_NULL) needs to be used as stated in the below answer
  • WesternGun
    WesternGun over 5 years
  • mliu
    mliu almost 3 years
    This was the right solution for me, as I didn't want to add annotations to my shared object classes