org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion
JPA
μ°κ΄κ΄κ³μμμλ°©ν₯ 맀ν
μ μ μΈν κ²½μ° λ°μ- 컨νΈλ‘€λ¬μμ
JSON
μΌλ‘ κ°μ μΆλ ₯νλ κ²½μ°, νμ μ λ³νν΄μΌ νλλ° λ³νλλ μν°ν°μ νλκ° λ€λ₯Έ μν°ν°λ₯Ό μ°Έμ‘°νκ³ λ κ·Έ μν°ν° ν΄λμ€μ νλκ° λ λ€λ₯Έ μν°ν°λ₯Ό μ°Έμ‘°νκ³ μInfinite Recursion
...
@Entity
@Getter
@Setter
@EqualsAndHashCode(of = "id")
@Builder @AllArgsConstructor @NoArgsConstructor
public class Member {
@Id
@GeneratedValue
private Long id;
@JsonManagedReference // μΆκ°ν λΆλΆ
@OneToMany(mappedBy = "member")
private List<Tour> tours = new ArrayList<>();
@JsonManagedReference
@OneToMany(mappedBy = "member")
private List<Scrap> scraps = new ArrayList<>();
@Column(unique = true)
private String email;
@Column(unique = true)
private String phone;
private String nickname;
private String name;
private LocalDate birth;
private String password;
private int teamIndex;
private boolean isAgeOlderThan14;
private boolean isAgreeOnTOS;
private boolean isAgreeOnGetPromotion;
private int gender;
@ColumnDefault("50")
private int weather;
}
package shop.spotweb.spot.domain;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.*;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@Setter
@EqualsAndHashCode(of = "id")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Tour {
@Id
@GeneratedValue
private Long id;
@JsonBackReference // μΆκ°ν λΆλΆ
@ManyToOne
private Member member;
private String tourLocation;
private String tourTeam;
private int tourStartDate;
private int tourEndDate;
private String tourTitle;
private String tourContent;
// ν¬μ΄ 쑰건
private int requiredGender; // 1μ΄λ©΄ λ¨μ± 2μ΄λ©΄ μ¬μ± 3μ΄λ©΄ μ ν μμ
private String tourTheme;
private int minimumAge;
private int maximumAge;
// μμ± μκ°
private LocalDateTime postsAt;
// private int numberOfView;
private int totalNumberOfMember;
private int nowNumberOfMember;
@JsonManagedReference
@OneToMany(mappedBy = "tour")
private List<Scrap> scraps = new ArrayList<>();
}
Member
μν°ν°(@OneToMany
)μ Tour
μν°ν°(@ManyToOne
)λ μλ‘ μλ°©ν₯ κ΄κ³μ΄λ€.
λ κ°μ²΄λ₯Ό μ¬μ©ν 컨νΈλ‘€λ¬λ JSON
μ λ°ννλ RestController
μΈλ°, μμμ μ²λΌ μ΄λ
Έν
μ΄μ
λ€μ λΆμ΄μ§ μμΌλ©΄ 무ν루νλ¬Έμ κ° λ°μνλ€. λ°λΌμ μλ°©ν₯ 맀νμ λ§Ίμ μν°ν°λ€μ μ¬μ©νλ RestControllerμμλ μλ λ κ°μ μ΄λ
Έν
μ΄μ
μ λΆμ¬μ€μΌνλ€.
@JsonManagedReference
- μ°Έμ‘°κ° λλ μλΆλΆμ μλ―Ένλ©°, μ μμ μΌλ‘ μ§λ ¬νλ₯Ό μννλ€.
Collection Type
μ μ μ©λλ€.@OneToMany
μ΄λ Έν μ΄μ μ λΆμΈλ€.
@JsonBackReference
- μ°Έμ‘°κ° λλ λ·λΆλΆμ μλ―Ένλ©°, μ§λ ¬νλ₯Ό μννμ§ μλλ€.
@ManyToOne
μ΄λ Έν μ΄μ μ λΆμΈλ€.