【发布时间】:2021-11-16 16:03:48
【问题描述】:
假设我有两个使用 spring webclient 的调用。两者的定义如下,在 .onStatus 中抛出异常:
public Mono<Model> getCall(String path) {
webClient.get()
.uri(path)
.retrieve()
.onStatus(HttpStatus::isError, errorHandler())}
这是异常函数errorHandler():
private Function<ClientResponse, Mono<? extends Throwable>> errorHandler() {
return clientResponse -> clientResponse.bodyToMono(ErrorResponse.class).
flatMap(errorBody -> Mono.error(new CustomException(clientResponse.statusCode().value(), "exception", errorBody)));
在我的 Mono.zip 中,我将其定义如下:
Mono.zip(getCall(call1),
getCall(call2))
.block;
问题是,如果其中一个调用引发异常,我将无法再获得至少一个结果。尝试使用:
.onErrorContinue(CustomException.class, (error, output) -> log.error("CustomException !" + error + output))
那么如何使用 Mono.zip 处理异常,就像我们使用 try catch 一样,但又不停止执行并获得成功调用的结果? 谢谢你
【问题讨论】:
标签: java spring-webflux reactive spring-webclient