Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JSON using Jackson in REST API Implementation with Spring Boot

  • Last Updated : 27 Nov, 2020

Whenever we are implementing a REST API with Spring (Spring Boot), we would have come across the requirement to exclude NULLs in the JSON Response of the API. Also, there might be a requirement to externalize turning ON/OFF this feature: Exclude NULLS in the JSON Response, thereby allowing the consumer of the API to customize as per the need.

In this article, we summarize the approach that used to implement the Externalization of ON/OFF the above-mentioned feature using Jackson – A java-based library to serialize or map java objects to JSON and vice versa.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Unsuccessful Approaches :

Initially, we tried with the Spring Boot’s readily available approaches, which were not successful, such as:

  • Having spring.jackson.default-property-inclusion property in This property takes values such as: always/non_null/non_absent/non_default/non_empty.
  • Extending WebMvcConfigurationSupport class and customize the Spring Boot configuration, see below.


class WebMvcConfiguration extends WebMvcConfiguration{
    protected void extendsMessageConverters
      (List<HttpMessageConverter<?>> converters)
        for(HttpMessageConverter<?> converter: converters)
            if(converter instnceof MappingJackson2HttpMessageConverter)
                ObjectMapper mapper = 

Now, Creating an instance of org.springframework.http.converter.json.Jackson2ObjectMapperBuilderCustomizer, Below the given code is for your reference.


Public Jackson2ObjectMapperBuilderCustomizer customJackson(){
    return new Jackson2ObjectMapperBuilderCustomizer(){
    public void customize(Jackson2ObjectMapperBuilder builder){

Successful Approach:

Create a JSON Response Object, if not created. This is the object which while serializing to JSON you want to ‘Exclude/Include Null fields’ feature based on the property in You can see below the Response object for your reference.


public class RegistrationResponse{
    private Boolean success = null;
    private String message = null;
    private String data = null;
    private Integer errorCode = null;
    public RegistrationResponse success(Boolean success){
        this.success = success;
        return this;
        public Boolean isSuccess(){
            return success;
        public void setSuccess(Boolean success){
            this.success = success;
        public RegistrationResponse message(String message){
            this.message = message;
            return this;
        public String getMessage(){
            return message;
        public void setMessage(String message){
            this.message = message;

Create a property by the name ‘config.response.json.format.exclude_null’ which can take values as ‘true’ or ‘false’. The value ‘true’ means to exclude null fields in the JSON Response & the value ‘false’ means to not exclude null fields. Also, bind this property to a class-level property so that the value can be read, Below given code is for your reference.

config.response.json.format.exclude_null = false
private boolean toExcludeNull;

Implement the actual logic to exclude nulls based on the global property value by – Creating an instance of com.fasterxml.jackson.databind.ObjectMapper.

  • Setting SerializationInclusion property of the created mapper to Include.NON_NULL if the global property value is true, otherwise set to Include.ALWAYS.
  • Serialize the Response object to a String using the mapper & return the String. Make sure to handle JsonProcessingException.

Let’s see below for the actual code snippet to be added while constructing the JSON Response as part of the API implementation.


RegistrationResponse regResp = new RegistrationResponse();
ObjectMapper mapper = new ObjectMapper()
if(toExcludeNull) mapper.setSerializationInclusion(Include.NON_NULL);
else mapper.serializationInclusion(Include.ALWAYS);
String regRespStr = null;
    regRespStr = mapper.writeValueAsString(regResp);
    catch(JsonProcessingException e)
    System.out.println("Formatted JSON Response:" + regRespStr);

Here, regRespStr is the JSON String with NULL exclusion/inclusion applied.

You can also check the Github Repository.

My Personal Notes arrow_drop_up
Recommended Articles
Page :