无法写入 JSON:Infinite 递归(StackOverflowError)嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:无限递归

Could not write JSON:Infinite recursion(StackOverflowError)nested exception is com.fasterxml.jackson.databind.JsonMappingException:Infinite recursion

我开发了 Spring Boot + Spring Data Jpa Rest 示例。我开发了以下代码并给出了以下错误,使用该代码即使我无法启动 Swagger 也给我错误。

{
    "timestamp": "2019-07-22T15:29:04.487+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Could not write JSON: Infinite recursion (WhosebugError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (WhosebugError) (through reference chain: java.util.ArrayList[0]->com.example.demo.entity.Employee[\"department\"]->com.example.demo.entity.Department$HibernateProxy$muKgohop[\"employees\"])",
    "path": "/employees/findEmployees/john"
}

RangeError: Maximum call stack size exceeded at Mt.map (immutable.js:4401) at e (utils.js:64) at immutable.js:3016 at immutable.js:2699 at ft.__iterate (immutable.js:2206) at Mt.__iterate (immutable.js:2698) at r.Lt.r.__iterateUncached (immutable.js:3015) at le (immutable.js:604) at r.J.__iterate (immutable.js:274) at r.forEach (immutable.js:4381)

日志:我可以看到很多像 belowm 这样的日志进入了递归。

2019-07-22 20:59:04.484  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@79e70d9e<rs=HikariProxyResultSet@1587950423 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.484  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@7c90a614<rs=HikariProxyResultSet@891782616 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.484  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@464e1dc3<rs=HikariProxyResultSet@1097620444 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.484  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@61b104db<rs=HikariProxyResultSet@1262879325 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.484  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@207b7e8e<rs=HikariProxyResultSet@318222175 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.484  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@59a2dc93<rs=HikariProxyResultSet@1365596257 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.485  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@6fb47261<rs=HikariProxyResultSet@718420456 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.485  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@6ca2490<rs=HikariProxyResultSet@205724777 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.485  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@7dbea5c9<rs=HikariProxyResultSet@1468691307 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.485  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@32b7b8a9<rs=HikariProxyResultSet@352269292 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.485  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@7edcf3e4<rs=HikariProxyResultSet@442667117 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.485  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@2965a60b<rs=HikariProxyResultSet@1042756846 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.485  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@21fdce03<rs=HikariProxyResultSet@764641401 wrapping Result set representing update count of 3>
2019-07-22 20:59:04.485  WARN 21016 --- [io-8080-exec-10] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@2e805865<rs=HikariProxyResultSet@1442507514 wrapping Result set representing update count of 3>

Employee.java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Employee implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="EMPLOYEE_ID")
    private Long employeeId;

    @Column(name="FIRST_NAME")
    private String firstName;

    @Column(name="LAST_NAME")
    private String lastName;

    @Column(name="EMAIL_ID")
    private String email;

    @Column(name="STATUS")
    private String status;

    @Column(name="BIRTH_DATE")
    private LocalDate birthDate;

    @Column(name="PROJECT_ASSOCIATION")
    private Integer projectAssociation;

    @Column(name="GOAL_COUNT")
    private Integer goalCnt;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "DEPT_ID", nullable = false)
    private Department department;
}

Department.java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Department implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="DEPT_ID")
    private Long departmentId;

    @Column(name="DEPT_NAME")
    private String departmentName;

    @Column(name="DEPT_CODE")
    private String departmentCode;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "department")
    private Set<Employee> employees = new HashSet<>(0);
}

休息端点

@GetMapping("/findEmployees/{searchValue}")
    public ResponseEntity<List<Employee>> findEmployees(@PathVariable("searchValue") String searchValue) {
        List<Employee> employees = employeeService.searchGlobally(searchValue);
        return new ResponseEntity<>(employees, HttpStatus.OK);
    }

我只是添加了 @JsonIgnore 并且它起作用了。

@OneToMany(fetch = FetchType.LAZY, mappedBy = "department")
@JsonIgnore
private Set<Employee> employees;

还有

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DEPT_ID", nullable = false)
@JsonIgnore
private Department department;

如果您想在父实体的 JSON 响应中保留值,您可以这样做:

//without @JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DEPT_ID", nullable = false)
private Department department;

还有

//with @JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "department")
@JsonIgnore
private Set<Employee> employees;

还有没有员工值的子实体中的@Override hashCode() 方法,如下所示:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((departmentId == null) ? 0 : departmentId .hashCode());
    result = prime * result + ((departmentName == null) ? 0 : departmentName.hashCode());
    result = prime * result + ((departmentCode == null) ? 0 : departmentCode.hashCode());
    return result;
}