Convert Flux<T> in Mono<Response<T>> in Spring Webflux

Viewed 1113

We are using Spring Data reactive and when we call findAll we are receiving a Flux<T> and we want to expose this response in the API, but our API structure is

{
    "status": 200,
    "items": [ ... ]
}

So, we want to expose this as Mono<<Response<T>>

public class Response<T> {
    private int status;
    private List<T> items;
    // ...
}

How to convert Flux<T> in Mono<Response<T>> in Spring Webflux?

1 Answers
Flux<User> users = //...
Mono<Response<User>> response = users.collectList().map(items -> new Response(items));
Related