StackOverflowError: null when doing a get request

StackOverflowError: null when doing a get request

当我尝试向我的 /users 端点发出 get 请求时遇到问题,而不是返回我想要的内容,returns 以下错误:

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 (WhosebugError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (WhosebugError) (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.WhosebugError: 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;
}

什么可能导致此错误,我该如何解决?

您必须将 @JsonIgnore 添加到 private Group group; 以便 jackson 可以忽略它。否则 jackson 将尝试序列化 group,然后它会创建一个从 Group 中的 Set<User> users 返回到 User 中的 group 属性 的循环] 对象。

通过分析报错信息,这部分具体为:

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

你可以使用@OneToMany(mappedBy =

@OneToMany(mappedBy = "group",cascade = CascadeType.ALL ,fetch = FetchType.LAZY)