gpt4 book ai didi

java - 如何将 Mono> 转换为 Stream

转载 作者:行者123 更新时间:2023-11-30 12:07:54 24 4
gpt4 key购买 nike

我有一段代码使用 WebClient 创建一个 Mono<List<T>>来自 Json 数组结果。 bodyToMono 方法返回 Mono<List<T>对象,我订阅它然后得到一个 parallelStream

    final WebClient client = WebClient.create(daemonEndpoint);
client.get()
.uri("/services?label=com.docker.stack.namespace")
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<List<Map<String, Object>>>() {
})
.subscribe(services -> services.parallelStream()
.map(e -> {
final String id = (String) e.get("ID");

我想知道是否有解决方案可以删除该订阅部分。

最佳答案

根据我使用 Reactor 的经验,你无法在不阻塞调用的情况下将 Mono 转换为 Stream,可以按如下方式完成:

Stream<T> stream = yourMono<T>.map(it -> it.parallelStream()).block()

另一种方法就是以 react 方式处理它(注意,无论如何有人必须订阅你的发布者,它不能自己完成):

yourMono<T>.flatMapMany(Flux::fromIterable)
.flatMap(it -> {
//there goes your <Map<String, Object>>
});

关于java - 如何将 Mono<List<T>> 转换为 Stream<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54614095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com