为了账号安全,请及时绑定邮箱和手机立即绑定

无法写入 JSON:无限递归(StackOverflowError)嵌套异常是

无法写入 JSON:无限递归(StackOverflowError)嵌套异常是

红糖糍粑 2023-04-13 10:37:51
我开发了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:在 Mt.map (immutable.js:4401) 在 e (utils.js:64) 在 immutable.js:3016 在 immutable.js:2699 在 ft.__iterate (immutable.js:2206)在 Mt.__iterate (immutable.js:2698) 在 r.Lt.r.__iterateUncached (immutable.js:3015) 在 le (immutable.js:604) 在 rJ__iterate (immutable.js:274) 在 r.forEach (immutable .js:4381)Employee.java@Builder@Data@AllArgsConstructor@NoArgsConstructor@Entitypublic 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;}
查看完整描述

2 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

我简单地添加了它@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;


查看完整回答
反对 回复 2023-04-13
?
守着一只汪

TA贡献1872条经验 获得超3个赞

如果您想在父实体的 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;

}


查看完整回答
反对 回复 2023-04-13
  • 2 回答
  • 0 关注
  • 196 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信