티스토리 뷰

반응형

 

 

문제 상황   

 

Article 목록을 뷰에다가 전달해주려는 그런 상황 

문제 요약   
@RequiredArgsConstructor
@Controller
public class BlogViewController {

    private final BlogService blogService;

    @GetMapping("/articles")
    public String getArticls(Model model) {
        List<ArticleListViewResponse> articles = blogService.findAll().stream()
                .map(ArticleListViewResponse::new)
                .toList();
        model.addAttribute("articles", articles);

        return "articleList";
    }
}

@ Controller 소스코드임 

 

저기서

.map(ArticleListViewResponse::new)

 

이 부분이 빨갛게 뜨길래 마우스를 올려다 보니까 

 



 

 

문제 해결 

 

@Getter
public class ArticleListViewResponse {

        private final long id;
        private final String title;
        private final String content;

        public ArticleListViewResponse(Long id ,String title, String content) {
            this.id = id;
            this.title = title;
            this.content = content; 
        }
}

 

이게 문제임 지금 Response 에서 Long, String, String 즉 AllArgsment 를 하고 있는데 

이게 아닌 객체를 넘겨야 했음 우린 생성하는 게 아니고 조회이니까... 

 

 

문제 해결   

 

@Getter
public class ArticleListViewResponse {

        private final long id;
        private final String title;
        private final String content;

        public ArticleListViewResponse(Article article) {
            this.id = article.getId();
            this.title = article.getTitle();
            this.content = article.getContent();
        }
}

 

 

반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함