05-05 05:52
Notice
Recent Posts
Recent Comments
«   2025/05   »
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
Archives
Today
Total
관리 메뉴

(주) 망나니 힘집

[SpringBoot] reason: Incompatible parameter types in method reference expression 해결 Resolve 본문

스펙도 쌓니

[SpringBoot] reason: Incompatible parameter types in method reference expression 해결 Resolve

군포망나니 2025. 1. 28. 22:53

 

 

문제 상황   

 

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();
        }
}