Ok, so I had the below method, but am trying to get away from using .block(), so attempting to implement Mono but getting error Required type: List<Mono<CatalogItem>> Provided:Mono<Object>
original code
@GetMapping("/user/{userId}")
public List<CatalogItem> getUserMoviesRating(@PathVariable String userId) {
UserRating ratings = webClientBuilder.build()
.get()
.uri("http://localhost:8083/ratingsdata/users/" + userId)
.retrieve()
.bodyToMono(UserRating.class)
.block();
assert ratings != null;
return ratings.getUserRating().stream().map(rating -> {
Movies movie = webClientBuilder.build()
.get()
.uri("http://localhost:8082/movies/" + rating.getMovieId())
.retrieve()
.bodyToMono(Movies.class)
.block();
assert movie != null;
return new CatalogItem(movie.getName(), movie.getDesc(), rating.getRating());
}).collect(Collectors.toList());
}}
My attempt to refactor using Mono in a non-blocking way
@GetMapping("/user/{userId}")
public List <Mono<CatalogItem>> getUserMoviesRating(@PathVariable String userId) {
Mono<UserRating> ratings = webClientBuilder.build()
.get()
.uri("http://localhost:8083/ratingsdata/users/" + userId)
.retrieve()
.bodyToMono(UserRating.class);
return ratings.map(userRating -> userRating.getUserRating().stream().map(rating -> {
Mono<Movies> movies = webClientBuilder.build()
.get()
.uri("http://localhost:8082/movies/" + rating.getMovieId())
.retrieve()
.bodyToMono(Movies.class);
return movies.map(m -> new CatalogItem(m.getName(), m.getDesc(), rating.getRating()));
}).collect(Collectors.toList()));
}