Jackson一对多多对一无限循环反序列化错误

时间:2017-12-27 12:40:14

标签: jackson spring-data spring-data-jpa

我有2个型号 -

Member.java

@Entity
@Table(name="members")
public class Member {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="member_id")
    private Integer id;

    @Column(name="member_name")
    @NotNull
    private String name;

    @Column(name="member_joining_date")
    @NotNull
    private Date joiningDate = new Date();

    @Enumerated(EnumType.STRING)
    @Column(name="member_type",columnDefinition="varchar(255) default 'ORDINARY_MEMBER'")
    private MemberType memberType = MemberType.ORDINARY_MEMBER;

    @OneToMany(mappedBy="tmod",cascade=CascadeType.ALL)
    private List<Meeting> meetingsAsTmod = new ArrayList<Meeting>();

    @OneToMany(mappedBy="tableTopicsMaster",cascade=CascadeType.ALL)
    private List<Meeting> meetingsAsTableTopicsMaster = new ArrayList<Meeting>();

    @OneToMany(mappedBy="generalEvaluator",cascade=CascadeType.ALL)
    private List<Meeting> meetingsAsGeneralEvaluator = new ArrayList<Meeting>();

    ...getters and setters, constructors...

}

Meeting.java

public class Meeting {

    @Id
    @GeneratedValue
    @Column(name="meeting_id")
    @NotNull
    private int meetingId;

    @Column(name="word")
    @NotNull
    private String word;

    @Column(name="theme")
    @NotNull
    private String theme;


    @Column(name="meeting_date")
    @NotNull
    private Date meetingDate;


    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="tmod_member_id")
    @NotNull
    private Member tmod;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="table_topics_master_member_id")
    @NotNull
    private Member tableTopicsMaster;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="general_evaluator_member_id")
    @NotNull
    private Member generalEvaluator;

    ...getters,setters,constructors...

我希望我的程序在meetingsAsTmod,meetingsAsTableTopicsMaster等中返回会议的ID,而不是会议列表,其中包含用户,其中包含会议列表并创建循环

如何在会议中显示会员的ID以及会员扮演该角色的会议的ID? (而不是完整的对象)使用杰克逊?

如果没有,我该如何解决这个问题?

PS - 我尝试使用@JsonIdentityInfo。但是,如果以前没有打印过,它会递归打印所有对象。而不是打印

{
"meetingId": 23,
"word": "meet2",
"theme": "meet2",
"meetingDate": 1514387641632,
"tmod": 4,
   ....
   ....
 }

打印 -

 {
"meetingId": 23,
"word": "meet2",
"theme": "meet2",
"meetingDate": 1514387641632,
"tmod": {
    "id": 4,
    "name": "Prateek4",
    "joiningDate": 1514375322337,
    "memberType": "ORDINARY_MEMBER",
    "meetingsAsTmod": [
        {
            "meetingId": 20,
            "word": "meet2",
       ....
        .....

1 个答案:

答案 0 :(得分:0)

将以下行添加到多对一和一对多映射

   @JsonIdentityInfo(generator = 
                    ObjectIdGenerators.PropertyGenerator.class,property = "id")
    @JsonIdentityReference(alwaysAsId=true)

将id更改为对象中的Id