首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring boot JPA:在self join relationShip中递归JSON-view

Spring boot JPA:在self join relationShip中递归JSON-view
EN

Stack Overflow用户
提问于 2019-12-11 05:23:03
回答 1查看 671关注 0票数 1

对于我正在尝试构建的项目,我需要一个从用户类到类用户的manyToMany关系。(用户有朋友,朋友是用户的朋友)。当试图用JPA让Json脱离SpringBoot的时候。我在JSON上得到一个递归循环(只有当两个用户是对方的朋友时才会发生这种情况)。

我知道会发生什么,但找不到解决问题的办法。如你所见,我使用不同的视图来“过滤”视图。问题是:我如何停止递归?

代码语言:javascript
复制
@Entity
public class User {
    @Id
    @GeneratedValue
    @JsonView(JsonViews.UserView.class)
    private long userId;
    @JsonView(JsonViews.UserView.class)
    private String username;
    @JsonView(JsonViews.UserView.class)
    private String password;
    @JsonView(JsonViews.UserView.class)
    private String email;
    @JsonView(JsonViews.UserView.class)
    private String location;
    //private String avatar;
    @JsonView(JsonViews.UserView.class)
    private int ranking;
    private String UserStatus;

    //Friend Relation Many > Many
    @JsonView({JsonViews.UserView.class, JsonViews.FriendWith.class})
    @ManyToMany()
    private List<User> friendsWith;
    @ManyToMany(mappedBy = "friendsWith")
    private List<User> friendOf;
    @JsonView({JsonViews.UserView.class, JsonViews.Player.class})
    @OneToMany(mappedBy = "user")
    private List<Player> player;

    public User() {
        this.friendsWith = new ArrayList<>();
        this.friendOf = new ArrayList<>();
    }

    public User(String username, String password, String email, String location, int ranking) {
        this();
        //this.userId = userId;
        this.username = username;
        this.password = password;
        this.email = email;
        this.location = location;
        this.ranking = ranking;

    }
// and th usual getters and setters

堆栈:

代码语言:javascript
复制
2019-12-10 22:17:52.414 ERROR 1618 --- [nio-8084-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() 
for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.hibernate.collection.internal.PersistentBag[0]->
nl.hva.ewa.stratego.backend.models.User["friendsWith"]->
org.hibernate.collection.internal.PersistentBag[0]->
nl.hva.ewa.stratego.backend.models.User["friendsWith"]->
org.hibernate.collection.internal.PersistentBag[0]->
nl.hva.ewa.stratego.backend.models.User["friendsWith"]->
org.hibernate.collection.internal.PersistentBag[0]->
nl.hva.ewa.stratego.backend.models.User["friendsWith"]->
ETC.... ETC.....

为了保证JsonViews类的完整性:

代码语言:javascript
复制
public class JsonViews {
    public class UserView { };
    public class AComplete extends UserView { };
    public class BComplete extends UserView { };
    public class FriendsWith extends UserView {};
    public class FriendsOf extends UserView {};

    public class Player extends UserView {};
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-11 18:15:47

尝试使用@JsonIdentityInfo注释,您可以告诉Jackson POJO id是什么,这样它就不会重复了:

代码语言:javascript
复制
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userId")
public class User {
    public long userId;
    public String name;
    public List<User> friends;
}

这将生成以下JSON:

代码语言:javascript
复制
  {
  "userId" : 11,
  "name" : "A",
  "friends" : [ {
    "userId" : 22,
    "name" : "B",
    "friends" : [ 11, {
      "userId" : 33,
      "name" : "C",
      "friends" : [ ]
    } ]
  } ]
}

注意,在内部用户B中,如果已经将用户添加到JSON body [ 11, {中,则好友列表仅包含ID。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59275551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档