首页 > 解决方案 > 我的流图中的错误是什么意思?

问题描述

错误:

 Error

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Jan 09 14:22:48 IST 2021 There was an unexpected error (type=Internal Server Error, status=500). Unresolved compilation problem: The method map(Function<? super Rating,? extends R>) in the type Stream is not applicable for the arguments (( rating) -> {}) java.lang.Error: Unresolved compilation problem: The method map(Function<? super Rating,? extends R>) in the type Stream is not applicable for the arguments (( rating) -> {})

at com.study.movie.controller.CatalogController.getCatalog(CatalogController.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at 

错误图片

代码:

return ratings.stream()
                .map(rating -> {
                    Movie movie=restTemplate.getForObject("http://localhost:8081/movies/"+rating.getMovieId(), Movie.class);
                    new CatalogItem(movie.getName(),movie.getDescription(),rating.getRating());
        })
        .collect(Collectors.toList());

标签: java

解决方案


lambda 表达式中的代码块不返回任何内容,但是.map()方法需要一个返回某些内容的函数/lambda。

您应该将代码编写为

return ratings.stream()
           .map(rating -> {
               Movie movie=restTemplate.getForObject("http://localhost:8081/movies/"+rating.getMovieId(), Movie.class);
               return new CatalogItem(movie.getName(),movie.getDescription(),rating.getRating());
           })
           .collect(Collectors.toList());

推荐阅读