对于我要构建的项目,我需要从用户类到类用户的多对多关系(用户有Friends,而Friends是Users的朋友)。当尝试使用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 {};
}
尝试使用@JsonIdentityInfo
批注,您可以在其中告诉杰克逊什么是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, {
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句