1. Overview

This article is about Spring Jackson exclude null fields or we can say Spring Jackson ignore the null value at the time of serialization. While using RestController in Spring Rest service at time response automatically converted to JSON using Jackson. If some field values are NULL then Jackson will be converted to NULL value by default as like:

{
  "no": 1,
  "name": null
}

If we use @JsonInclude(JsonInclude.Include.NON_NULL) at the Class level or Field level then it excludes the NULL value field as like:

{
  "no": 1
}

2. Spring Jackson exclude null fields

1. Spring Jackson exclude null field level

 @JsonInclude(JsonInclude.Include.NON_NULL) configuration at the field level, If the field contains NULL value then it will exclude that field from JSON.

  • @JsonInclude(JsonInclude.Include.NON_NULL) for excluding NULL values
  • @JsonInclude(JsonInclude.Include.NON_EMPTY) for excluding “” (empty String) or NULL
package com.javadeveloperzone.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.io.Serializable;

public class Employee implements Serializable{

    private int no;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String name;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getNo() { return no; }
    public void setNo(int no) { this.no = no; }
}

2. Spring Jackson exclude null class level

 @JsonInclude(JsonInclude.Include.NON_NULL) configuration at the class level, If any of field inside that class contains NULL value then that field will be excluded from JSON.

  • @JsonInclude(JsonInclude.Include.NON_NULL) for excluding NULL values
  • @JsonInclude(JsonInclude.Include.NON_EMPTY) for excluding “” (empty String) or NULL
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Employee implements Serializable{

    private int no;

    private String name;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getNo() { return no; }
    public void setNo(int no) { this.no = no; }
}

3. Spring Jackson exclude null global configuration

We can also configuration globally using  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); so any of JSON conversation if NULL value found then that field will be excluded from JSON.

Create ObjectMapper bean and set mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

NOTE: Make sure that bean is inside the java configuration file.

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);  // NON_EMPTY for '' or NULL value
    return mapper;
}

3. Spring Jackson exclude null in Spring boot after 1.4.x

spring.jackson.default-property-inclusion is spring properties using that we can also exclude null fields:

application.properties

spring.jackson.default-property-inclusion=non_null

3. Conclusion

Here we learned that way to exclude or empty null field from JSON in spring Jackson, We learned different ways exclude at field level or class level and also provide a way to implement global level using ObjectMapper or spring.jackson.default-property-inclusion in spring boot application.

4. References

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *