Jackson ObjectMapper setSerializationInclusion() not working
<p>I'm just getting familiar with Jackson binding. However, when I'm testing setSerializationInclusion(JsonInclude.Include.NON_NULL), I found that it's not working sometimes.</p>

<p>Here is my code</p>

<pre><code>package com.blithe.main;

import com.blithe.model.Student;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Jackson_2_NullValue {
    public static void main(String[] args) throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();


        Student s = new Student();
        String stundetString = mapper.writeValueAsString(s);
        System.out.println(stundetString);


        // exclude null fields
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        s.setName("ss");
        stundetString = mapper.writeValueAsString(s);
        System.out.println(stundetString);
    }
}
</code></pre>

<p>and the POJO</p>

<pre><code>package com.blithe.model;

import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

// @JsonIgnoreProperties(ignoreUnknown = true)
// exclude null fields for the whole class
// @JsonInclude(Include.NON_NULL)
public class Student {

    // exclude the field whe it's empty ("")
    // @JsonInclude(value=Include.NON_EMPTY)
    private String name;

    private Integer age;

    private Date birth;

    // Jackson ignores it
    @JsonIgnore
    private String nickName;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}
</code></pre>

<p>the output is </p>

<pre><code>{"name":null,"age":null,"birth":null}
{"name":"ss","age":null,"birth":null}
</code></pre>

<p>The later one should be null-value excluded, but it doesn't.</p>

<p>However, when I put my code this way.</p>

<pre><code>package com.blithe.main;

import com.blithe.model.Student;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Jackson_2_NullValue {
    public static void main(String[] args) throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        Student s = new Student();
        String stundetString = mapper.writeValueAsString(s);
        System.out.println(stundetString);


        // exclude null fields
        // mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        s.setName("ss");
        stundetString = mapper.writeValueAsString(s);
        System.out.println(stundetString);
    }
}
</code></pre>

<p>It works with the output below</p>

<pre><code>{}
{"name":"ss"}
</code></pre>

<p>Is this normal or just some kind of bug? Do I miss anything? The only maven dependency is jackson-databind 2.7.4. Any discussion is welcomed. Thanks!</p>