如何从 Mono 中的列表中获取 Flux?

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

我有一个 Mono 对象,其中包含一个列表。我需要从 Mono 中取出该列表并将其放入 Flux 中。

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

findById 方法采用聊天 ID 和 returns Mono<Chat>,其中 Chat 包含消息列表。我想获取该消息列表并将其转换为 Flux<Messages>

您可以通过以下方式简单地使用 Mono#flatMapMany and Flux#fromIterable 方法的组合

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