作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一段代码使用 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/
我是一名优秀的程序员,十分优秀!