스펙도 쌓니

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

 

 

반응형