簡體   English   中英

如何轉換單聲道 <List<T> &gt;串流 <T> ?

[英]How to convert Mono<List<T>> to Stream<T>?

我有一個使用WebClient從Json數組結果創建Mono<List<T>>的代碼。 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()

另一種方式是以反應方式處理它(請注意,無論如何有人必須訂閱您的發布者,它本身不能完成)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM