我们如何将 Mono<List<Type>> 转换为 Flux<Type>
How do we convert a Mono<List<Type>> to a Flux<Type>
使用spring5,有了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)
请查找以下代码:
第一次将 Mono<List<String>>
转换为 Flux<List<String>>
并将 Flux<List<String>>
转换为 Flux<String>
List<String> asList = Arrays.asList("A", "B", "C", "D");
Flux<String> flatMap = Mono.just(asList).flux().flatMap(a-> Flux.fromIterable(a));
或者您可以使用 flatmapmany
:
Flux<String> flatMapMany = Mono.just(asList).flatMapMany(x-> Flux.fromIterable(x));
使用spring5,有了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)
请查找以下代码:
第一次将 Mono<List<String>>
转换为 Flux<List<String>>
并将 Flux<List<String>>
转换为 Flux<String>
List<String> asList = Arrays.asList("A", "B", "C", "D");
Flux<String> flatMap = Mono.just(asList).flux().flatMap(a-> Flux.fromIterable(a));
或者您可以使用 flatmapmany
:
Flux<String> flatMapMany = Mono.just(asList).flatMapMany(x-> Flux.fromIterable(x));