The Jackson @JsonInclude註解可以在特定條件下用於排除類別的屬性或欄位,並可以使用JsonInclude.Include枚舉來定義。 JsonInclude.Include枚舉包含一些常數,如"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 }
以上是在Java中,Jackson @JsonInclude註解的重要性是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!