이 문제는 많은 시간을 요구하고 해결책은 꽤 분명하지만, 제 경우에는 실제로 작동하지 않습니다. 그것을 해결하는 방법을 알아 내야합니다.Spring MVC + Hibernate & Jackson = JSON을 작성할 수 없음 : 무한 재귀
문제 : Could not write JSON: Infinite recursion (StackOverflowError)
셋업은 다음과 같이이다 :
직원은 부서에 속한다. (ManyToOne) 부서에는 관리자로 종업원이 있습니다. (OneToOne)
부서에 @OneToMany 목록을 갖고 싶지 않아 소유자 측이 누락되었습니다. 직원에게는 부서 개체가 있지만이 부서는 그가 관리하는 부서가 아닌 해당 부서에 속합니다.
Employee.java
@Entity
@Table(name = "ts_employee")
//@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,property="@emp_id")
public class Employee extends AbstractEntity {
@ManyToOne
@JoinColumn(name = "dept_id")
@JsonManagedReference
private Department department;
... getters and setters
}
Department.java
@Entity
@Table(name = "ts_department")
//@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@dept_id")
public class Department extends AbstractEntity {
@OneToOne
@JoinColumn (name = "manager_id")
@JsonBackReference
private Employee manager;
.. other fields.. getters and setters
}
AbstractEntity.java
@MappedSuperclass
public class AbstractEntity implements Serializable {
@Id
@GeneratedValue()
private Long id;
... getters and setters
}
내가했습니다 응답으로 전송
@JsonBackReference + @JsonManagedReference 나는 StackOverflow의 제거하지만, Department.manager이 (@JsonBackReference)를 직렬화되지 않으며,하지, 나쁜 년 : 두 솔루션을 시도했다.
@JsonIdentityInfo (발전기 = ObjectIdGenerators.IntSequenceGenerator.class, 재산 = "@의 EMP_ID") 아무것도하지 않는 것, 그리고 StackOverflow에 내 얼굴 :(
방법에 발생합니다, 나는 희망 모델 클래스를 수정하지 않고,이 문제를 해결할 수 있습니까?
덕분에 많이 :)
귀하의 컬렉션에 @JsonIgnore가이 예외를 수정한다는 사실을 알고 있습니다. – JamesENL
Department.java에서 @JsonIdentityInfo 접근 방식을 시도했으며 제대로 작동하도록 솔기가 있습니다. 도와 주셔서 정말 고맙습니다. :) – user3159152
당신이 당신의 문제를 해결했기 때문에 기쁩니다. 그 답이 도움이된다면, 동의로 표시하십시오. –