강의

멘토링

커뮤니티

Inflearn Community Q&A

heartrose1696's profile image
heartrose1696

asked

[Revised 2023-11-27] Developing RESTful Web Services using Spring Boot 3.x

Setting the relationship between Post Entity and User Entity for post viewing

무한반복 질문

Written on

·

494

1

안녕하세요.

강의를 따라하다가 이전강의에서 해놓은 Posts를 추가하고 

/jpa/users/900001/posts 를 하면  아래와 같은 에러가

발생하면서 동일한 결과를 계속 만들어 냅니다.

"Could not write JSON: Infinite recursion (StackOverflowError)"

혹시 제가 잘못 셋팅한 부분이 있을까요???

스프링 버전은 2.4입니다.

rest-apispring-boot

Answer 3

0

@JsonManagedReference
@OneToMany(mappedBy = "user")
private List<Post> posts;


@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnore
private User user;

무한 반복이 없어 졌습니다.

0

Dowon Lee님의 프로필 이미지
Dowon Lee
Instructor

안녕하세요, 이도원입니다. 

문의하신 질문은 Users와 Posts 사이에서 양방향 JSON 변환이 반복되어서 생기는 현상같습니다. 

Users에서 Posts의 정보를 가져와서 JSON으로 변환하고, Posts에서는 Posts를 작성한 Users의 정보를 다시 JSON으로 변환하는 무한 반복이 생기는 현상입니다. 

Users에 @JsonManagedReference, Posts에 @JsonBackReference를 설정하시거나, Posts에서 JSON 변환을 무시하는 @JsonIgnore 어노테이션을 추가해 보시면 해결 될것 같습니다. 

감사합니다. 

0

heartrose1696님의 프로필 이미지
heartrose1696
Questioner

https://pasudo123.tistory.com/350

위 게시글 보고 양방향 무한순환참조현상 해결했습니다 

근데 왜 선생님 예제에서는 무환참조현상이 안일어난걸까요...

heartrose1696's profile image
heartrose1696

asked

Ask a question