If you're using Jackson to serialize a Java object in your Spring Boot application, you might want it to skip the null values.

Why? Because otherwise you end up with a JSON object that has a bunch of properties with null values as well. 

And that creates a bit of a clutter.

Fortunately, Jackson offers an easy way to ignore nulls.

I'll show you how to do it here.

Use @JsonInclude

It might not make any sense at first blush that you'd want to use an annotation with the word "include" in it to exclude something, but that's exactly what you need to do.

The @JsonInclude annotation is what you're looking for.

But you need to use that annotation in such a way that you tell Jackson what to inlcude. And then Jackson will ignore everything else.

You see where this is going?

A demonstration is in order.

Let's take a look at a mini version of an Employee class:

public class Employee {

    private String id;    
    private String lastName;
    private String firstName;    
    private String anniversaryDate;
    
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getAnniversaryDate() {
        return anniversaryDate;
    }

    public void setAnniversaryDate(String anniversaryDate) {
        this.anniversaryDate = anniversaryDate;
    }

    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}

That class includes four (4) properties: the employee's ID, first name, last name, and wedding anniversary date.

Of course, not all employees will have a wedding anniversary date because not all employees are married. So that field might be null.

Now let's serialize an instance of that Employee class with a null anniversary date:

ObjectMapper mapper = new ObjectMapper();            

Employee employee = new Employee();
employee.setId("A17");
employee.setFirstName("Frank");
employee.setLastName("Smith");

System.err.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee));

And that gives us this output:

{
  "id" : "A17",
  "lastName" : "Smith",
  "firstName" : "Frank",
  "anniversaryDate" : null
}

The "anniversaryDate" property is null.

But most applications don't need to see a null property because null is implied. Also, as I mentioned earlier, null properties in large objects create clutter.

So let's clean this up.

Cleaning It Up

Annotate the Employee class as follows:

@JsonInclude(Include.NON_NULL)
public class Employee {
...

There's that @JsonInclude annotation I teased earlier.

But, as you can see, it's not alone. Check out the stuff in the parentheses.

That Include enum tells Jackson which properties get included in the serialization process. The name NON_NULL is pretty descriptive.

It only includes properties that aren't null.

Now rerun that serialization code again and you'll see this result:

{
  "id" : "A17",
  "lastName" : "Smith",
  "firstName" : "Frank"
}

There you go. No null properties.

By the way: you could use Include.NON_EMPTY for this purpose but that would also exclude empty strings and zero-length arrays.

Wrapping It Up

Now you know how to exclude null values during the Jackson serialization process.

Over to you. Take what you've learned here and put it in your own Spring Boot applications.

Have fun!

Photo by Paula Schmidt from Pexels