JsonManagedReference vs JsonBackReference

后端 未结 5 2133
一向
一向 2020-11-27 04:42

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

5条回答
  •  醉梦人生
    2020-11-27 04:56

    • @JsonManagedReference -> Manages the forward part of the reference and the fields marked by this annotation are the ones that get Serialised
    • @JsonBackReference -> Manages the reverse part of the reference and the fields/collections marked with this annotation are not serialised.

    Use case: You have a one-many or many-many relationships in your entities/tables and not using the above would lead to errors like

    Infinite Recursion and hence stackoverflow - > Could not write content: Infinite recursion (StackOverflowError)
    

    The above errors occurs because Jackson (or someother similiar) tries to serialise both ends of the relationship and ends up in a recursion.

    @JsonIgnore performs similiar functions but the above mentioned annotations are preferable.

提交回复
热议问题