Home Map Mono<Resources<Resource<T>>> to Mono<List<T>>?
Reply: 1

Map Mono<Resources<Resource<T>>> to Mono<List<T>>?

Jan Nielsen
1#
Jan Nielsen Published in 2018-01-13 01:40:16Z

In my reactive Java 9 Spring Boot 2 with Spring HATEOAS application, I have a REST API returning Mono<Resources<Resource<T>>>. In the application layer, I'd like the data to be Mono<List<T>> but how can I concisely map Mono<Resources<Resource<T>>> to Mono<List<T>>?

I was expecting something like:

public Mono<List<Order>> orders() {
    return rest.orders()
        .flatmap(List::stream)
        .map(r -> r.getContent())
        .collect(Collectors.toList());
}

but this fails to compile because List does not define List.stream(Resources<Resource<Order>>. The rest.orders() returns Mono<Resources<Resource<Order>>>. Any ideas?

Jan Nielsen
2#
Jan Nielsen Reply to 2018-01-14 14:28:22Z

A (memory-sloshing) solution:

public Mono<List<Order>> orders() {
    return rest.orders()
        .map(r -> toList(r));
}

private List<Order> toList(Resources<Resource<Order>> resources) {
    List<Order> orders = new ArrayList<>();
    resources.forEach(r -> orders.add(r.getContent());
    return orders;
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.38541 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO