How @JsonInclude(Include.NON_NULL) works In Spring Boot

10,584

Solution 1

What that annotation means is that when an object is deserialized whether to include only non-null fields on not.

When you are using it for an object that you are getting as an input, even if the input fields are null, because the variables name and email are class level fields, they will be inited to their default value which is null.

So when you create your return statement you are always going to get null for those.

Try this annotation with a JSON object as a response and then try populating some fields and keeping some fields null and then see the response to various calls. This will help you understand the effects of this annotation.

Solution 2

@JsonInclude(Include.NON_NULL) or @JsonInclude(JsonInclude.Include.NON_NULL) is used to ignore null fields in an object. In your particular example you have returned a String value that is why it is printing null. If you try to return the complete object, then you will find that the null fields are not included in the response body

Share:
10,584
AlejoDev
Author by

AlejoDev

I'm funny to java web development

Updated on June 05, 2022

Comments

  • AlejoDev
    AlejoDev almost 2 years

    I am trying to understand what is the reason of existence @JsonInclude to use it in my DTOs. Let's look at this simple example:

    A Code:

    class DemoApplication {
        static void main(String[] args) {
            SpringApplication.run DemoApplication, args
        }
        @PostMapping("/")
        String greet(@RequestBody Greeting greeting) {
            return "Hello ${greeting.name}, with email ${greeting.email}"
        }
    }
    
    @JsonInclude(JsonInclude.Include.NON_NULL)
    class Greeting {
      String name
      String email
    }
    

    B Code:

    class DemoApplication {
        static void main(String[] args) {
            SpringApplication.run DemoApplication, args
        }
        @PostMapping("/")
        String greet(@RequestBody Greeting greeting) {
            return "Hello ${greeting.name}, with email ${greeting.email}"
        }
    }
    
    class Greeting {
      String name
      String email
    }
    

    The only difference between the A code and the B code is that in the B code the greeting class does not use the @JsonInclude annotation.

    But if I do some simple CURL requests to that endpoint (A code and B code):

    ~ curl -H "Content-Type: application/json" -X POST localhost:8080
    {"timestamp":"2018-04-22T21:18:39.849+0000","status":400,"error":"Bad Request","message":"Required request body is missing: public java.lang.String com.example.demo.DemoApplication.greet(com.example.demo.Greeting)","path":"/"}
    ~ curl -H "Content-Type: application/json" -X POST localhost:8080  -d '{}'
    Hello null, with email null                                                                     
    ~ curl -H "Content-Type: application/json" -X POST localhost:8080  -d '{"name": "AlejoDev"}'
    Hello AlejoDev, with email null                                                                    
    ~ curl -H "Content-Type: application/json" -X POST localhost:8080  -d '{"name": "AlejoDev", "email":"[email protected]"}'
    Hello AlejoDev, with email [email protected]
    

    Get the same behavior, then what is the utility of using @JsonInclude annotation?

    For example, I expected that in A code when I sent a request with only one field, like this:

    ~ curl -H "Content-Type: application/json" -X POST localhost:8080  -d '{"name": "AlejoDev"}'
    

    the Greeting object became an object with only one field, and not an object with two fields. one full and one empty.