gpt4 book ai didi

java - 无法写入 JSON :Infinite recursion(StackOverflowError)nested exception is com. fastxml.jackson.databind.JsonMappingException:无限递归

转载 作者:行者123 更新时间:2023-12-02 09:40:05 28 4
gpt4 key购买 nike

我开发了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"
}

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)

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

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;

关于java - 无法写入 JSON :Infinite recursion(StackOverflowError)nested exception is com. fastxml.jackson.databind.JsonMappingException:无限递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57149468/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com