1

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

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

@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

堆:

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 类:


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 {};
}
4

1 回答 1

1

试试@JsonIdentityInfo注解,这里可以告诉Jackson POJO id 是什么,所以就不再重复了:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userId")
public class User {
    public long userId;
    public String name;
    public List<User> friends;
}

这将生成以下 JSON:

  {
  "userId" : 11,
  "name" : "A",
  "friends" : [ {
    "userId" : 22,
    "name" : "B",
    "friends" : [ 11, {
      "userId" : 33,
      "name" : "C",
      "friends" : [ ]
    } ]
  } ]
}

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

于 2019-12-11T10:15:46.657 回答