gpt4 book ai didi

spring - 我们如何将 Mono> 转换为 Flux

转载 作者:行者123 更新时间:2023-12-01 09:14:53 30 4
gpt4 key购买 nike

使用 spring 5 和 reactor 我们有以下需求。

Mono<TheResponseObject> getItemById(String id){
return webClient.uri('/foo').retrieve().bodyToMono(TheResponseObject)
}

Mono<List<String>> getItemIds(){
return webClient.uri('/ids').retrieve().bodyToMono(List)
}

Mono<RichResonseObject> getRichResponse(){
Mono<List> listOfIds = Mono.getItemIds()
listOfIds.each({ String id ->
? << getItemById(id) //<<< how do we convert a list of ids in a Mono to a Flux
})
Mono<Object> someOtherMono = getOtherMono()
return Mono.zip((? as Flux).collectAsList(), someOtherMono).map({
Tuple2<List, Object> pair ->
return new RichResonseObject(pair.getT1(), pair.getT2())
}).cast(RichResonseObject)
}

What approaches are possible to convert a Mono<List<String>> to a Flux<String>?

最佳答案

这应该有效。给定 Mono 中的字符串列表

  Mono<List<String>> listOfIds;

Flux<String> idFlux = listOfIds
.flatMapMany(ids -> Flux.fromArray(ids.toArray(new String [0])));

更好的是
listOfIds.flatMapMany(Flux::fromIterable)

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

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