- 엔티티를 JSON으로 변환하는 도중에 에러가 발생
- 양방향 연관관계 설정시 지연로딩으로 설정된 엔티티를 API에서 내려줄 때, Jackson이 데이터를 변환하다가 알 수 없는 타입이라는 에러가 발생.
@Entity
@Getter
@NoArgsConstructor
public class Book {
@Id
@Column(name = "ISBN")
private String isbn;
@Column(name = "BOOK_NAME")
private String bookName;
@Column(name = "AUTHOR")
private String author;
@Column(name = "PUBLISHER")
private String publisher;
@Column(name = "KDC")
private String kdc;
@Column(name = "CATEGORYy")
private String category;
@Column(name = "KEYWORD")
private String keyword;
@Column(name = "BOOK_IMAGE")
private String img;
// @JsonIgnore //bookReviewList 를 조회하지 않는다..... 쓰면 좋지 않다..... 쓰지마라
@OneToMany(mappedBy = "book")
private List<BookReview> bookReviewList = new ArrayList<>();
}
@Getter
@Entity
@NoArgsConstructor
public class BookReview {
@Id
@GeneratedValue
private long reviewNo;
private int rating;
private String reviewContents;
private LocalDateTime createDate;
private int declaration;
//NORMAl, BLIND
@Enumerated(EnumType.STRING)
private ReviewStatus reviewStatus;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_no")
private UserInfo userInfo;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "isbn")
private Book book;
}
@Repository
public interface BookReviewRepository extends JpaRepository<BookReview,String> {
List<BookReview> findAll();
}
public BookReviewResponse readReviewAll() {
log.info("showReview");
String hMessage = null;
Object data = null;
StatusEnum hCode = null;
try {
List<BookReview> bookReviewList = bookReviewRepository.findAll();
hCode = StatusEnum.hd1004;
hMessage = "가져오기";
data = bookReviewList;
} catch (Exception e) {
log.error("createReview err :: error msg : {}", e);
hCode = "11111";
hMessage = "readReview 에러";
data = null;
}
return BookReviewResponse.builder()
.data(data)
.hCode(hCode)
.hMessage(hMessage)
.build();
}
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError);
@JsonIgnore //bookReviewList 를 호회하지 않는다
@OneToMany(mappedBy = "book")
private List<BookReview> bookReviewList = new ArrayList<>();
Hibernate5Module Been이용
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-hibernate5'
@Bean
Hibernate5Module hibernate5Module(){
Hibernate5Module hibernate5Module = new Hibernate5Module();
return hibernate5Module;
}
jackson-datatype-hibernate5
모듈을 추가하고 Bean 등록을 하게 되면, 지연로딩으로 설정된 프록시 엔티티를 null 값으로 설정한다.@Data
public class BookReviewDto {
private int rating;
private String reviewContents;
private LocalDateTime createDate;
private int declaration;
private String isbn;
private String userName;
// private UserInfo userInfo;
// private Book book;
public BookReviewDto(BookReview bookReview) {
rating = bookReview.getRating();
reviewContents = bookReview.getReviewContents();
createDate = bookReview.getCreateDate();
declaration = bookReview.getDeclaration();
isbn = bookReview.getBook().getIsbn();
userName = bookReview.getUserInfo().getUserName();
// book = bookReview.getBook();
// userInfo = bookReview.getUserInfo();
}
}
List<BookReview> bookReviewList = bookReviewRepository.findAll();
List<BookReviewDto> result = bookReviewList.stream()
.map(r -> new BookReviewDto(r))
.collect(Collectors.toList());
log.info("bookReviewList : {}", result.toString());
hCode = StatusEnum.hd1004;
hMessage = "가져오기";
data = result;