Ignore null value Jackson JSON serialization

Ignore null value Jackson JSON serialization

If you are using Jackson mapper to serialize data and want to ignore null value during serialization please use below:

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

Above will work for Jackson version >1.9

package com.javahonk;

import org.codehaus.jackson.map.annotate.JsonSerialize;

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Person {
	
	private String firstName;
	private String lastName;
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}

Other solution if above doesn’t work:

final ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Inclusion.NON_NULL);

OR:

@JsonInclude(Include.NON_NULL)
public class Person {
	
	private String firstName;
	private String lastName;
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}

For more information on Jackson please use this tutorial

One thought on “Ignore null value Jackson JSON serialization”

Leave a Reply

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