ar.thewaterfall
ar.thewaterfall

Reputation: 835

How to get Flux out of list that is inside Mono?

I've got a Mono with an object that contains a list. I need to get that list out of Mono and put it inside Flux.

public Flux<Message> getMessages(String id) {
        return chatDAO.findById(id);
}

That findById method takes an id of a chat and returns Mono<Chat> where Chat has a list of messages. I want to take that list of messages and convert it into Flux<Messages>

Upvotes: 1

Views: 771

Answers (1)

Serhii Pov&#237;senko
Serhii Pov&#237;senko

Reputation: 3906

you can simply use a combination of Mono#flatMapMany and Flux#fromIterable methods in the following way

public Flux<Message> getMessages(String id) {
    return chatDAO.findById(id)
                  .map(Chat::getMessages) //assumes that you have getter for your messages
                  .flatMapMany(Flux::fromIterable);
}

Upvotes: 3

Related Questions