Jackson Annotations - @JsonManagedReference



Overview

@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.

Creating Parent-Child Relationship

Annotate Parent

Here we're defining Student object as owner of the book and annotating with @JsonManagedReference.

class Book {
   public int id;
   public String name;
   ...
   @JsonManagedReference
   public Student owner;
}

Annotate Child

Here we're defining List<Book> object as child of the student and annotating it using @JsonBackReference.

class Student {
   public int rollNo;
   public String name;

   @JsonBackReference
   public List<Book> books;
   ...
}

Example - Serialization with @JsonManagedReference

JacksonTester.java

package com.tutorialspoint;

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;
}

Output

Run the JacksonTester and verify the output −

{
  "id" : 1,
  "name" : "Learn HTML",
  "owner" : {
    "rollNo" : 1,
    "name" : "Mark"
  }
}

Here we can see, using @JsonManagedReference and @JsonBackReferences annotations, we're able to define parent-child relationship.

Advertisements