해결된 질문
작성
·
398
0
[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? 예
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? 예
3. 질문 잘하기 메뉴얼을 읽어보셨나요? 예
[질문 내용]
21:59초에 영상 내용을 따라 Lazy 초기화 후에 포스트맨을 검색해보았는데 출력이 되지 않고 오류가 떠서 질문드립니다.. 오류를 보아하니 무한루프가 돌아가는것 같은 느낌도 듭니다..
오류내용
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
2023-10-24 12:44:32.150 ERROR 46125 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : 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); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->
initDb
package jpabook.jpashop;
import jpabook.jpashop.domain.*;
import jpabook.jpashop.domain.Item.Book;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
@Component
@RequiredArgsConstructor
public class InitDb {
private final InitService initService;
@PostConstruct
public void init() {
initService.dbInit1();
initService.dbInit2();
}
@Component
@Transactional
@RequiredArgsConstructor
static class InitService {
private final EntityManager em;
public void dbInit1() {
Member member = createMember("userA", "서울", "1", "1111");
em.persist(member);
Book book1 = createBook("JPA1 BOOK", 10000, 100);
em.persist(book1);
Book book2 = createBook("JPA2 BOOK", 20000, 100);
em.persist(book2);
OrderItem orderItem1 = OrderItem.createOrderItem(book1, 10000, 1);
OrderItem orderItem2 = OrderItem.createOrderItem(book2, 20000, 2);
Order order = Order.createOrder(member, createDelivery(member),
orderItem1, orderItem2);
em.persist(order);
}
public void dbInit2() {
Member member = createMember("userB", "진주", "2", "2222");
em.persist(member);
Book book1 = createBook("SPRING1 BOOK", 20000, 200);
em.persist(book1);
Book book2 = createBook("SPRING2 BOOK", 40000, 300);
em.persist(book2);
Delivery delivery = createDelivery(member);
OrderItem orderItem1 = OrderItem.createOrderItem(book1, 20000, 3);
OrderItem orderItem2 = OrderItem.createOrderItem(book2, 40000, 4);
Order order = Order.createOrder(member, delivery, orderItem1,
orderItem2);
em.persist(order);
}
private Member createMember(String name, String city, String street,
String zipcode) {
Member member = new Member();
member.setName(name);
member.setAddress(new Address(city, street, zipcode));
return member;
}
private Book createBook(String name, int price, int stockQuantity) {
Book book = new Book();
book.setName(name);
book.setPrice(price);
book.setStockQuantity(stockQuantity);
return book;
}
private Delivery createDelivery(Member member) {
Delivery delivery = new Delivery();
delivery.setAddress(member.getAddress());
return delivery;
}
}
}
OrderSimpleApiController
package jpabook.jpashop.api;
import jpabook.jpashop.domain.Order;
import jpabook.jpashop.repository.OrderRepository;
import jpabook.jpashop.repository.OrderSearch;
import lombok.RequiredArgsConstructor;
import org.aspectj.weaver.ast.Or;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* XToOne
* Order
* Order -> Member
* Order -> Delivery
*/
@RestController
@RequiredArgsConstructor
public class OrderSimpleApiController {
private final OrderRepository orderRepository;
@GetMapping("/api/v1/simple-orders")
public List<Order> ordersV1(){
List<Order> all = orderRepository.findAllByString(new OrderSearch());
for (Order order : all) {
order.getMember().getName(); //Lazy 강제 초기화
order.getDelivery().getAddress(); //Lazy 강제 초기화
}
return all;
}
}
JpashopApplication
package jpabook.jpashop;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class JpashopApplication {
public static void main(String[] args) {
SpringApplication.run(JpashopApplication.class, args);
}
@Bean
Hibernate5Module hibernate5Module(){
Hibernate5Module hibernate5Module = new Hibernate5Module();
return hibernate5Module;
}
}
코드도 같이 올렸습니다.. 무엇이 다른건지 잘 모르겠습니다..
감사합니다!! JsonIgnore 가 필요한 것이었군요