Jackson Annotations Tutorial
Selected Reading
- Jackson - Discussion
- Jackson - Useful Resources
- Jackson - Quick Guide
- Disable Annotation
- MixIn Annotations
- Custom Annotation
- Jackson - @JsonFilter
- Jackson - @JsonIdentityInfo
- @JsonBackReference
- @JsonManagedReference
- Jackson - @JsonView
- Jackson - @JsonUnwrapped
- Jackson - @JsonFormat
- Jackson - @JsonProperty
- Jackson - @JsonTypeName
- Jackson - @JsonSubTypes
- Jackson - @JsonTypeInfo
- Jackson - @JsonAutoDetect
- Jackson - @JsonInclude
- Jackson - @JsonIgnoreType
- Jackson - @JsonIgnore
- @JsonIgnoreProperties
- @JsonEnumDefaultValue
- Jackson - @JsonDeserialize
- Jackson - @JsonSetter
- Jackson - @JsonAnySetter
- Jackson - @JacksonInject
- Jackson - @JsonCreator
- Jackson - @JsonSerialize
- Jackson - @JsonRootName
- Jackson - @JsonValue
- Jackson - @JsonRawValue
- @JsonPropertyOrder
- Jackson - @JsonGetter
- Jackson - @JsonAnyGetter
- Jackson - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
@JsonManagedReference
Jackson Annotations - @JsonManagedReference
@JsonManagedReferences and JsonBackReferences are used to display objects with parent child relationship. @JsonManagedReferences is used to refer to parent object and @JsonBackReferences is used to mark child objects.
Example - @JsonManagedReferences
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; pubpc class JacksonTester { pubpc 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 { pubpc int rollNo; pubpc String name; @JsonBackReference pubpc List<Book> books; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; this.books = new ArrayList<Book>(); } pubpc void addBook(Book book){ books.add(book); } } class Book { pubpc int id; pubpc String name; Book(int id, String name, Student owner){ this.id = id; this.name = name; this.owner = owner; } @JsonManagedReference pubpc Student owner; }
Output
{ "id" : 1, "name" : "Learn HTML", "owner" : { "rollNo" : 1, "name" : "Mark" } }Advertisements