@Override
public DetailedPostResponse getPost(Long postId, User user) {
Post post = getPostById(postId);
List<UserPost> userPosts = getUserPostsByPost(post);
List<List<Menu>> allMenus = getMenus(userPosts,post);
return DetailedPostResponse.builder()
.author(getAuthor(userPosts))
.address(post.getAddress())
.store(post.getStore())
.minPrice(post.getMinPrice())
.deliveryCost(post.getDeliveryCost())
.participants(getParticipants(userPosts))
.myMenus(getMyMenus(user, allMenus))
.menus(allMenus)
.sumPrice(getSumPrice(getUserPostsByPost(post),post))
.deadline(getDeadline(post))
.build();
}
{
"status": 200,
"message": "글 상세페이지 조회에 성공했습니다.",
"data": {
"author": "가나다라",
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"myMenus": [
{
"id": 1,
"post": {
"id": 1,
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"category": "KOREAN",
"menus": [
{
"id": 1,
"post": {
"id": 1,
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"category": "KOREAN",
"menus": [
{
"id": 1,
"post": {
"id": 1,
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"category": "KOREAN",
"menus": [
{
"id": 1,
"post": {
"id": 1,
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"category": "KOREAN",
"menus": [
{
"id": 1,
"post": {
"id": 1,
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"category": "KOREAN",
"menus": [
{
"id": 1,
"post": {
"id": 1,
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"category": "KOREAN",
"menus": [
{
"id": 1,
"post": {
"id": 1,
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"category": "KOREAN",
"menus": [
{
"id": 1,
"post": {
"id": 1,
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"category": "KOREAN",
"menus": [
"deliveryCost": 2000,
"category": "KOREAN",
... 이하 반복 후 에러메시지가 출력됨 "menus": [
{
"status": 400,
"message": "에러가 발생했습니다",
"data": "Could not write JSON: Infinite recursion (StackOverflowError)"
}
myMenus가 문제임
private List<Menu> getMyMenus(User user, List<List<Menu>> allMenus){
List<Menu> myMenus = null;
for(List<Menu> menus : allMenus){
if(menus.size()>0){
if(menus.get(0).getUser().getId().equals(user.getId())){
myMenus = menus;
break;
}
}
}
return myMenus;
}
저기에서 뭐가 오류가 나는지 모르겠네?
break가 안 되는건가 싶어서 break대신 return을 해봄
private List<Menu> getMyMenus(User user, List<List<Menu>> allMenus){
for(List<Menu> menus : allMenus){
if(menus.size()>0){
if(menus.get(0).getUser().getId().equals(user.getId())){
return menus;
}
}
}
return null;
}
상황이 똑같음
mymenus가 아니라 allmenus가 문제인가 싶어서 순서를 한번 바꿔봄
@Builder
public record DetailedPostResponse (
String author,
String address,
String store,
Integer minPrice,
Integer deliveryCost,
List<List<Menu>> menus,
List<Menu> myMenus,
List<String> participants,
Integer sumPrice,
LocalDateTime deadline
){
}
return DetailedPostResponse.builder()
.author(getAuthor(userPosts))
.address(post.getAddress())
.store(post.getStore())
.minPrice(post.getMinPrice())
.deliveryCost(post.getDeliveryCost())
.participants(getParticipants(userPosts))
.menus(allMenus)
.myMenus(getMyMenus(user, allMenus))
.sumPrice(getSumPrice(getUserPostsByPost(post),post))
.deadline(getDeadline(post))
.build();
음 바꾸니까 allMenus가 문제였다는 걸 알게됨
{
"status": 200,
"message": "글 상세페이지 조회에 성공했습니다.",
"data": {
"author": "가나다라",
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"menus": [
[
{
"id": 1,
"post": {
"id": 1,
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"category": "KOREAN",
"menus": [
{
"id": 1,
"post": {
"id": 1,
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"category": "KOREAN",
"menus": [
{
"id": 1,
"post": {
"id": 1,
"address": "주소주소",
"store": "storestore",
"minPrice": 20000,
"deliveryCost": 2000,
"category": "KOREAN",
"menus": [
지금 allMenus가 어떻게 되고 있냐면
List<List<Menu>> allMenus = getMenus(userPosts,post);
private List<List<Menu>> getMenus(List<UserPost> userposts, Post post){
List<List<Menu>> menus = new ArrayList<>();
for(UserPost userpost : userposts){
List<Menu> myMenus = getUserMenus(userpost.getUser(),post);
menus.add(myMenus);
}
return menus;
}
private List<Menu> getUserMenus(User user, Post post){
return menuRepository.findAllByUserAndPost(user,post);
}
음… 왜 무한반복이 되는거지…
List<List<Menu>> menus = new ArrayList<>(); -> 미부분을 이렇게 쓰면 알아서 안 되는건가?
List<List<Menu>> menus = new ArrayList<List<Menu>>(); 이렇게 바꿔봄