杰克逊序列化:已经有了POJO for id

时间:2016-03-15 11:32:02

标签: json rest spring-mvc jackson entity

我需要帮助。我试图找到解决方案很长一段时间,我几乎阅读了互联网上关于杰克逊JSON序列化的所有内容 @JsonIdentityInfo注释但我无法弄清楚为什么会出现这个错误。

我有用户(可以发布多个请求,可以批准多个请求), 评论(属于一个请求,由一个用户发布)和 请求。

我收到错误:

go

用户类:

Could not read document: Already had POJO for id (java.lang.Long) [[ObjectId: key=1, type=com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator, scope=hr.svgroup.sipa.model.User]] (through reference chain: hr.svgroup.sipa.model.Comment[\"request\"]->hr.svgroup.sipa.model.Request[\"user\"]->hr.svgroup.sipa.model.User[\"id\"]);

评论类:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id",
scope = User.class)
@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToMany(mappedBy = "approvers")
    private List<Request> approvalRequests;

    @OneToMany(mappedBy = "user")
    private List<Request> createdRequests;

    @OneToMany(mappedBy = "user")
    private List<Comment> comments;

    ...
}

请求类:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id",
    scope = Comment.class)
@Entity
@Table(name = "comment")
public class Comment {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "request", nullable = false)
    private Request request;

    @ManyToOne
    @JoinColumn(name = "user", nullable = false)
    private User user;  
    ...
}

我正在使用杰克逊2.6.5。

谢谢!

0 个答案:

没有答案