The Jackson @JsonInclude annotation can be used to exclude properties or fields of a class under certain conditions, and It can be defined using the JsonInclude.Include enumeration. JsonInclude.Include enum Contains some constants, such as "ALWAYS", "NON_DEFAULT", "NON_EMPTY" and "NON_NULL", used to determine whether to exclude attributes (fields).
public static enum JsonInclude.Include extends Enum<JSonInclude.Include>
import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class JsonIncludeTest { public static void main(String args[]) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); Employee emp = new Employee(); String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp); System.out.println(jsonString); } } // Employee class @JsonInclude(JsonInclude.Include.NON_EMPTY) class Employee { public int empId = 115; public String empName = null; @Override public String toString() { return "Employee{" + "empId=" + empId + ", empName='" + empName + '\'' + '}'; } }
{ "empId" : 115 }
The above is the detailed content of What is the importance of Jackson @JsonInclude annotation in Java?. For more information, please follow other related articles on the PHP Chinese website!