spring-data-neo4j 的自身关系在 REST 中的深度太多

Too many depths in REST result for self relationship of spring-data-neo4j

我有一个自己的关系(一个员工可以向经理汇报,一个经理可能有很多员工)如下:

@NodeEntity
public class Employee {

    @GraphId
    private Long id;

    private String name;
    private String sn;
    private String mail;

    public Employee() {
        super();
    }

    public Employee(String name, String sn, String mail) {
        super();
        this.name = name;
        this.sn = sn;
        this.mail = mail;
    }

    @Relationship(type="REPORT_TO", direction=Relationship.OUTGOING)
    private Employee manager;

    @Relationship(type="REPORT_TO", direction=Relationship.INCOMING)
    private Set<Employee> staffs = new HashSet<>();

    ...
}

我使用 REST API 作为我的应用程序的界面。当我尝试 return all Employee with session.loadAll(Employee.class, 0) 时,似乎一切正常,但不会显示经理或员工信息。另一方面,如果我使用 session.loadAll(Employee.class, 0),那么 Employee 的许多深度都是 returned.

例如,如果员工 A 向员工 B 报告,即。 B 是 A 的经理。深度 0 的结果是:

{ "_embedded" : { "employees" : [ { "name" : "B", "sn" : "10001", "mail" : "B@test.com", "manager" : null, "staffs" : [ ], }, { "name" : "A", "sn" : "10000", "mail" : "A@test.com", "manager" : null, "staffs" : [ ], } ] } ... }

而深度 1 的结果是: [{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com",...

我该如何处理?有谁能给我一些建议吗

这是一个 JSON 序列化问题。实体包含循环引用,序列化程序无法检测到它们,因此它进入了构造 JSON 对象的无限循环。你可以试试JSOG Jackson plugin, which is able to detect and automatically handle this kind of problem. For an example of how it can be used in a Spring project, have a look at SDN University