📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Dependencies
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
- jackson-annotations-2.9.8.jar
- jackson-core-2.9.8.jar
- jackson-databind-2.9.8.jar
Table of contents
- @JsonInclude Annotation Overview
- Using Include.NON_NULL and Include.NON_EMPTY at the Property level
- Using Include.NON_NULL and Include.NON_EMPTY at the Class level
- Using Include.NON_NULL and Include.NON_EMPTY globally with ObjectMapper
1. @JsonInclude Annotation Overview
- Include.NON_NULL: Indicates that only properties with not null values will be included in JSON.
- Include.NON_EMPTY: Indicates that only properties that are not empty will be included in JSON.
2. Using Include.NON_NULL and Include.NON_EMPTY at the Property level
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
public class Employee {
private int id;
@JsonInclude(Include.NON_NULL)
private String firstName;
@JsonInclude(Include.NON_EMPTY)
private String lastName;
public Employee(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonIncludeAnnotationTest {
public static void main(String[] args) throws JsonProcessingException {
// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Employee employee = new Employee(10, null, "");
String result = mapper.writeValueAsString(employee);
System.out.println(result);
}
}
{
"id" : 10
}
3. Using Include.NON_NULL and Include.NON_EMPTY at the Class level
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@JsonInclude(Include.NON_NULL)
public class Employee {
private int id;
private String firstName;
@JsonInclude(Include.NON_EMPTY)
private String lastName;
public Employee(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonIncludeAnnotationTest {
public static void main(String[] args) throws JsonProcessingException {
// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Employee employee = new Employee(10, null, "Fadatare");
String result = mapper.writeValueAsString(employee);
System.out.println(result);
}
}
{
"id" : 10,
"lastName" : "Fadatare"
}
4. Using Include.NON_NULL and Include.NON_EMPTY globally with ObjectMapper
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.setSerializationInclusion(Include.NON_EMPTY);
package net.javaguides.jackson.annotations;
public class Employee {
private int id;
private String firstName;
private String lastName;
public Employee(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonIncludeAnnotationTest {
public static void main(String[] args) throws JsonProcessingException {
// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.setSerializationInclusion(Include.NON_EMPTY);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Employee employee = new Employee(10, null, "");
String result = mapper.writeValueAsString(employee);
System.out.println(result);
}
}
{
"id" : 10
}
Related Articles
- Change Field Name in JSON using Jackson (popular)
Comments
Post a Comment
Leave Comment