user13674325
user13674325

Reputation: 389

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

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?

Upvotes: 2

Views: 2348

Answers (1)

Brian Clozel
Brian Clozel

Reputation: 59056

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

Upvotes: 3

Related Questions