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

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

@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;

}

红糖糍粑
浏览 197回答 2
2回答

摇曳的蔷薇

我简单地添加了它@JsonIgnore并且它工作了。@OneToMany(fetch = FetchType.LAZY, mappedBy = "department")@JsonIgnoreprivate Set<Employee> employees;还@ManyToOne(fetch = FetchType.LAZY)@JoinColumn(name = "DEPT_ID", nullable = false)@JsonIgnoreprivate 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")@JsonIgnoreprivate Set<Employee> employees;以及没有员工值的子实体中的 @Override hashCode() 方法,如下所示:@Overridepublic int hashCode() {&nbsp; &nbsp; final int prime = 31;&nbsp; &nbsp; int result = 1;&nbsp; &nbsp; result = prime * result + ((departmentId == null) ? 0 : departmentId .hashCode());&nbsp; &nbsp; result = prime * result + ((departmentName == null) ? 0 : departmentName.hashCode());&nbsp; &nbsp; result = prime * result + ((departmentCode == null) ? 0 : departmentCode.hashCode());&nbsp; &nbsp; return result;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java