- Jackson 注解教程
- Jackson - 主页
- 序列化注解
- Jackson - @JsonAnyGetter
- Jackson - @JsonGetter
- @JsonPropertyOrder
- Jackson - @JsonRawValue
- Jackson - @JsonValue
- Jackson - @JsonRootName
- Jackson - @JsonSerialize
- 反序列化注解
- Jackson - @JsonCreator
- Jackson - @JacksonInject
- Jackson - @JsonAnySetter
- Jackson - @JsonSetter
- Jackson - @JsonDeserialize
- @JsonEnumDefaultValue
- 属性包含注解
- @JsonIgnoreProperties
- Jackson - @JsonIgnore
- Jackson - @JsonIgnoreType
- Jackson - @JsonInclude
- Jackson - @JsonAutoDetect
- 类型处理注解
- Jackson - @JsonTypeInfo
- Jackson - @JsonSubTypes
- Jackson - @JsonTypeName
- 通用注解
- Jackson - @JsonProperty
- Jackson - @JsonFormat
- Jackson - @JsonUnwrapped
- Jackson - @JsonView
- @JsonManagedReference
- @JsonBackReference
- Jackson - @JsonIdentityInfo
- Jackson - @JsonFilter
- 其他
- 自定义注解
- MixIn 注解
- 禁用注解
- Jackson 注解资源
- Jackson - 快速指南
- Jackson - 有用的资源
- Jackson - 讨论
Jackson 注解 - @JsonBackReference
@JsonManagedReferences 和 JsonBackReferences 用于显示具有父子关系的对象。@JsonManagedReferences 用于引用父对象,@JsonBackReferences 用于标记子对象。
示例 - @JsonBackReferences
import java.io.IOException; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]) throws IOException, ParseException { ObjectMapper mapper = new ObjectMapper(); Student student = new Student(1, "Mark"); Book book1 = new Book(1,"Learn HTML", student); Book book2 = new Book(1,"Learn JAVA", student); student.addBook(book1); student.addBook(book2); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(book1); System.out.println(jsonString); } } class Student { public int rollNo; public String name; @JsonBackReference public List<Book> books; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; this.books = new ArrayList<Book>(); } public void addBook(Book book){ books.add(book); } } class Book { public int id; public String name; Book(int id, String name, Student owner) { this.id = id; this.name = name; this.owner = owner; } @JsonManagedReference public Student owner; }
输出
{ "id" : 1, "name" : "Learn HTML", "owner" : { "rollNo" : 1, "name" : "Mark" } }
广告