Spring 启动 Webclient - 等待多调用的结束响应

Spring Boot Webclient - wait end response of multi call

我多次调用休息端点并使用 jpa 将响应保存在数据库中。收到所有响应后,我必须调用数据库中的存储过程。

我正在尝试使用网络客户端来执行此请求,但我不知道如何wait/check所有请求都已完成。

这是我用来调用端点的代码:

private Mono<DSGetDataResponse> GetData(DSGetDataRequest dataRequest){
try {
   return
    weblicent.post()
    .uri("GetData")              
        .contentType(MediaType.APPLICATION_JSON)
    .accept(MediaType.APPLICATION_JSON_UTF8)
        .syncBody(dataRequest)
        .retrieve()
        .bodyToMono(DSGetDataResponse.class);
} catch (Exception e) {
log.info("Sono in catch (Exception");
e.printStackTrace();
return null;
        }
    }

这是我用来调用上述方法的代码

Items_to_request().forEach( x -> {
 String token = tokenResponse.getTokenValue();
DSGetDataRequest dataRequest = new DSGetDataRequest(token, this.Richista_DS(x), null);
try{
    this.GetData(dataRequest).subscribe(dataResponse ->   this.handlerGetDataResponse(dataResponse));
}
catch (Exception e)
{log.info("Sono in catch (Exception");
e.printStackTrace();
}
});

在 handlerGetDataResponse 上,我只将响应保存在数据库中。

如何等待所有请求完成后调用数据库中的存储过程?

我知道我将无阻塞调用与阻塞调用混为一谈

你有什么解决问题的建议吗?

谢谢 米尔科

我已经使用 Flux 重写了您的代码

Flux.fromIterable(Items_to_request())
.flatMap( x -> {
    String token = tokenResponse.getTokenValue();
    DSGetDataRequest dataRequest = new DSGetDataRequest(token, this.Richista_DS(x), null);
    return this.GetData(dataRequest));
})
.onErrorResume(<add your logic>)
.collectList()
.subscribe(List<Data> data -> {< do what you want here})

您必须使用 onErrorResume 或 onErrorContinue 或类似的运算符正确处理错误,否则 flux 将终止。检查 here

此代码将触发所有请求并将响应收集到一个列表中,然后订阅它。