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

THE Waterfall :

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>

povisenko :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=328864&siteId=1