Skip to content

Latest commit

Β 

History

History
130 lines (97 loc) Β· 3.5 KB

Infinite-Recursion.md

File metadata and controls

130 lines (97 loc) Β· 3.5 KB

[error] Infinite Recursion 였λ₯˜

였λ₯˜ μ½”λ“œ

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...

ν•΄κ²°

Member Entity

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

}

Tour Entity

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 μ–΄λ…Έν…Œμ΄μ…˜μ— 뢙인닀.