我有一个客户端,它使用 webClient 将批次发送到第三方 API 以翻译一些文本。它看起来像这样:
TranslateCustomAnswersResponse fetchResponseCollection(List<TranslationPlatformRequest> platformRequestList){
Map<String, TranslatedCustomAnswer> translationMapSync = new ConcurrentHashMap<>();
Flux.fromIterable(platformRequestList)
.flatMap(list ->
getTranslationPlatformResponse(list)
.doOnNext(platformResponse ->
translationMapSync.
putAll(translationPlatformResponseToTranslateCustomAnswerResponseMapper.
map(platformResponse.getData()).getInitialValueToTranslatedAnswerMap())))
.blockLast();
return TranslateCustomAnswersResponse.builder().initialValueToTranslatedAnswerMap(translationMapSync).build();
}
因此,随着每个批次的返回结果被添加到构建器将用于构建响应的映射中。现在,我有一个服务需要异步调用同一个客户端,因此我想我应该从我的客户端返回一个 Mono,以便服务可以解包结果(该服务将调用另一个需要并行发生的 API,但如果我的客户阻塞它并不完全是异步的)。有没有办法让我的客户返回 Mono 和服务来解包结果,而无需推送任何填充地图的代码并构建对服务的最终响应?