无法写入 JSON:Infinite recursion(StackOverflowError)nested excep



我开发了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 (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (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"
}

范围错误:超出最大调用堆栈大小 在地图山(不可变.js:4401( 在 E (utils.js:64( 不可变.js:3016 在不可变.js:2699 at ft.__iterate (不可变.js:2206( at Mt.__iterate (不可变.js:2698( at r.Lt.r.__iterateUncached (不可变.js:3015( at le (不可变.js:604( at r.J.__iterate (不可变.js:274( at r.forEach (不可变.js:4381(

日志

我可以看到很多像下面这样的日志进入递归。

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>

员工.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;
}

部门.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;
}

相关内容

  • 没有找到相关文章

最新更新