Java中Jackson @ JsonInclude注释的重要性?
Jackson @JsonInclude注解可以用于在特定条件下排除类的属性或字段,并且可以使用JsonInclude定义它。包括枚举。JsonInclude。Include enum包含几个常量,如"ALWAYS","NON_DEFAULT", "NON_EMPTY"和"NON_NULL",以确定是否排除该属性(字段)。
语法
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 }