对于我正在尝试构建的项目,我需要一个从用户类到类用户的manyToMany关系。(用户有朋友,朋友是用户的朋友)。当试图用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 {};
}发布于 2019-12-11 18:15:47
尝试使用@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 body [ 11, {中,则好友列表仅包含ID。
https://stackoverflow.com/questions/59275551
复制相似问题