Spring Boot userRepository.findAll()显示太多次并导致stackoverflow错误

时间:2018-10-15 17:00:00

标签: java json spring-boot jackson

我有这段代码,但是我在浏览器上的输出不是我想要的

@GetMapping(path = "/all")
public @ResponseBody List<User> getAllUsers() {
    return userRepository.findAll();
}

输出:

[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users":[{"id":4,"firstName":"busayo","lastName":"amowe","email":"amoweolubusayo@gmail.com","password":"$2a$10$09kGDhlEp.Xmj0ocDjGLTO.03z.kGQKm2ZHcsNetDpH0r6m1rkVIu","roles":[{"id":1,"name":"ROLE_ADMIN","users"

不是应该打印一次吗?它说StackOverFlow错误,我的getJson函数无法显示它。我该怎么办?

1 个答案:

答案 0 :(得分:1)

您在useruser role实体之间具有循环依赖性。

为了将您的回复返回给客户端,您的List<User>必须被序列化。您必须告诉Jackson如何处理周期,否则它将因堆栈溢出异常而失败。

public class User {

    @JsonBackReference
    public List<Role> roles;

    // ...       

}

public class Role {

    @JsonManagedReference
    public List<User> users;

    // ...       

}

有关双向关系的Jackson教程,请参见https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion