Jackson: Multiple back-reference properties with name 'defaultReference'

后端 未结 4 2008
半阙折子戏
半阙折子戏 2021-02-11 19:01

I\'m trying to map a json (string format) to an object and I get the following error

com.fasterxml.jackson.databind.JsonMappingException: Multiple back-

相关标签:
4条回答
  • 2021-02-11 19:26

    I think the best way to handle this is using @JsonIdentityInfo annotation.see the thread which demonstrate this.How to use @JsonIdentityInfo with circular references?

    0 讨论(0)
  • 2021-02-11 19:27

    I also faced this issue, and resolved it. You should name all JsonManagedReferences and JsonBackReference in your application.

    example : @JsonManagedReference(value="user-person") @JsonBackReference(value="user-person")

    0 讨论(0)
  • 2021-02-11 19:28

    If you use @JsonBackReference on more than one getter/setter method in your project, you should distinguish them with a specific reference name.

    Maybe only one 'defaultReference' is allowed in the latest version?

    e.g

    In MovementView.java

    @JsonBackReference(value="user-movement")
    public User getUser() {
        return user;
    }
    

    In User.java

    @JsonManagedReference(value="user-movement")
    public MovementView getMovementView() {
        return movementView;
    }
    
    0 讨论(0)
  • 2021-02-11 19:30

    I also faced this issue, but resolved it.

    //This is parent class
    @Entity
    @Table(name = "checklist")
    @JsonIgnoreProperties("inspection")
    public class Checklist implements java.io.Serializable {
    
        @ManyToOne
        @JoinColumn(name = "product_id", referencedColumnName = "id")
        @JsonBackReference
        private Product product;
    
        @OneToMany(mappedBy = "checklists", cascade = CascadeType.ALL)
        @JsonManagedReference
        private Set<Inspection> inspection = new HashSet<Inspection>();
    //Constructor
    //Getter and Setter
    }
    
    //This is child class
    @Entity
    @Table(name = "inspections")
    public class Inspection {
    
        @ManyToOne
        @JoinColumn(name = "chk_id", referencedColumnName = "id")
        private Checklist checklists;
    //Constructor
    //Getter and Setter
    }
    

    By mentioning @JsonIgnoreProperties("inspection") and @JsonManagedReference.

    Resolved the issue raised by using two @JSONBackRefrence in same parent class.

    0 讨论(0)
提交回复
热议问题