javajackson

JsonManagedReference vs JsonBackReference


I would like to know the difference between @JsonManagedReference and @JsonBackReference in Jackson?


Solution

  • @JsonManagedReference is the forward part of reference – the one that gets serialized normally.

    @JsonBackReference is the back part of reference – it will be omitted from serialization.

    So they depend on the direction of your relationship:

    public class User {
        public int id;
        public String name;
     
        @JsonManagedReference
        public List<Item> userItems; 
    } 
    
    public class Item {
        public int id;
        public String itemName;
     
        @JsonBackReference
        public User owner; 
     }
    

    A useful guide for more details: https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion