Jackson:多个名为“defaultReference”的反向引用属性

2022-09-01 05:08:34

我正在尝试将json(字符串格式)映射到对象,但收到以下错误

com.fasterxml.jackson.databind.JsonMappingException:多个名为“defaultReference”的反向引用属性

这是 json 字符串

{"pledge":"74","client":"66","date":"","originId":"1","qualityId":"2","grade":"19","packing":"4","tons":"1000","fromDate":"","toDate":"","type":0,"remark":"","status":0,"area":"1531","id":-1,"refNumber":"","log":"","user":""}

这是对象

@Entity
@Table(name="movement", catalog = "wsmill3")
public class MovementView implements java.io.Serializable {
    private Integer id;
    private Integer originId;
    private Integer qualityId;
    private String refNumber;
    private Integer client;
    private String clientRef;
    private Integer grade;
    private Integer packing;
    private Integer pledge;
    private Integer area;
    private Date date;
    private Double tons;
    private Date fromDate;
    private Date toDate;
    private String remark;
    private User user;
    private Byte status;
    private String log;
    private Byte type;
    //constructor, getter and setter

这是做映射的代码

String data = request.getParameter("data");
ObjectMapper mapper = new ObjectMapper();
MovementView movement = mapper.readValue(data, MovementView.class);

我不知道这个错误,我做了与我在杰克逊主页上阅读完全相同的方式。任何知道它的人,请帮助我


答案 1

如果在项目中使用多个 getter/setter 方法,则应使用特定的引用名称来区分它们。@JsonBackReference

也许在最新版本中只允许一个?'defaultReference'

例如

MovementView.java

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

User.java

@JsonManagedReference(value="user-movement")
public MovementView getMovementView() {
    return movementView;
}

答案 2

我认为处理这个问题的最好方法是使用@JsonIdentityInfo注释。请参阅演示这一点的线程。如何将@JsonIdentityInfo与循环引用一起使用?


推荐