Home > database >  Mono Object instead of Mono<myPojo>
Mono Object instead of Mono<myPojo>

Time:10-19

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

CodePudding user response:

It is not an optimal solution to combine java 8 Stream API with Reactor publishers, because this makes code less readable and they work in differently. You can have a look here for more details In case if userRating.getUserRating() returns a list of ratings you can try this code.

  public Mono <List<CatalogItem>> getUserMoviesRating(@PathVariable String userId) {

    Mono<UserRating> ratings = webClientBuilder.build()
        .get()
        .uri("http://localhost:8083/ratingsdata/users/"   userId)
        .retrieve()
        .bodyToMono(UserRating.class);

    return ratings.flatMapMany(userRating -> Flux.fromIterable(userRating.getUserRating()))
        .flatMap(rating->this.getMovie(rating.getMovieId()))
        .map(m -> new CatalogItem(m.getName(), m.getDesc(), rating.getRating()))
        .collectList();
  }
    
  private Mono<Movies> getMovie(String movieId){
    return webClientBuilder.build()
        .get()
        .uri("http://localhost:8082/movies/"   movieId)
        .retrieve()
        .bodyToMono(Movies.class);
  }
  • Related