当我尝试对/ users端点执行get请求时遇到问题,而不是返回我想要的内容,而是在下面返回此错误:
2018-07-07 17:00:06.636 ERROR 294108 --- [nio-8080-exec-3] 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: io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"])] with root cause
java.lang.StackOverflowError: null
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_171]
at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~
我的端点方法:
@Override
public ResponseEntity<List<M>> findAll() {
List<M> models = repository.findAll();
if(models == null || models.isEmpty()){
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(models);
}
用户模型:
@Entity
@Table(name = "UN_USERS")
public class User extends AbstractModel {
@Column(name = "USERNAME", unique = true)
private String username;
@Column(name = "UUID")
private String uuid;
@Column(name = "COINS")
private long coins;
@Column(name = "TOKENS")
private long tokens;
@ManyToOne
@JoinColumn(name = "GROUP_ID", nullable = true)
private Group group;
//getters & setters...
}
组模型:
@Entity
@Table(name="UN_GROUPS")
public class Group extends AbstractModel{
private String prefix;
@OneToMany(mappedBy = "group")
private Set<User> users;
@ManyToMany(mappedBy = "groups")
private Set<Permission> permissions;
}
什么可能导致此错误,我该如何解决?
答案 0 :(得分:2)
通过分析错误消息,这部分专门:
through reference chain: io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]
我们得到了参考链形成一个循环。您已经遇到了Jackson无限递归问题,有几种解决方法。您可以找到一篇不错的文章,其中涵盖了处理方法here
答案 1 :(得分:1)
您必须将@JsonIgnore
添加到private Group group;
,以便杰克逊可以忽略它。否则,杰克逊将尝试序列化group
,然后将创建一个从group
对象中的User
返回到Set<User> users
中的Group
属性的循环。 / p>
答案 2 :(得分:1)
您可以使用@OneToMany(mappedBy =
@OneToMany(mappedBy = "group",cascade = CascadeType.ALL ,fetch = FetchType.LAZY)