The Jackson @JsonIgnoreType 註解可以在序列化過程中用來忽略一個類別,並且可以標記一個類別的所有屬性或字段在序列化和反序列化JSON物件時被忽略。
@Target(value={ANNOTATION_TYPE,TYPE}) @Retention(value=RUNTIME) public @interface JsonIgnoreType
import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class JsonIgnoreTypeTest { public static void main(String args[]) throws IOException { Employee emp = new Employee(); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp); System.out.println(jsonString); } } // Employee class class Employee { <strong> </strong>@JsonIgnoreType<strong> </strong> public static class Address { public String firstLine = null; public String secondLine= null; public String thirdLine = null; @Override public String toString() { return "Address{" + "firstLine='" + firstLine+ '\'' + ", secondLine='" + secondLine+ '\'' + ", thirdLine='" + thirdLine + '\'' + '}'; } } // end of Address class public long empId = 115; public String empName = "Raja Ramesh"; public Address empAddress = new Address(); <strong> </strong>@Override public String toString() { return "Employee{" + "empId=" + empId + ", empName='" + empName + '\'' + ", empAddress=" + empAddress + '}'; } }
{ "empId" : 115, "empName" : "Raja Ramesh" }
以上是如何在Java中使用Jackson忽略序列化過程中的某個類別?的詳細內容。更多資訊請關注PHP中文網其他相關文章!