如何在 WebClient 反应器中添加或忽略特定的 http 状态代码?
How add or Ignoring a specific http status code in WebClient reactor?
我有一个管理项目所有请求的方法:
private Mono<JsonNode> postInternal(String service, String codPayment, String docNumber, RequestHeadersSpec<?> requestWeb) {
return requestWeb.retrieve().onStatus(HttpStatus::is4xxClientError,
clientResponse -> clientResponse.bodyToMono(ErrorClient.class).flatMap(
errorClient -> clientError(service, codPayment, clientResponse.statusCode(), docNumber, errorClient)))
.onStatus(HttpStatus::is5xxServerError,
clientResponse -> clientResponse.bodyToMono(ErrorClient.class)
.flatMap(errorClient -> serverError(service, codPayment, docNumber, errorClient)))
.onRawStatus(value -> value > 504 && value < 600,
clientResponse -> clientResponse.bodyToMono(ErrorClient.class)
.flatMap(errorClient -> otherError(service, codPayment, docNumber, errorClient)))
.bodyToMono(JsonNode.class);
}
但是我使用状态代码 432 响应的 API 之一,当响应正常但其中有一个特殊条件时,我必须显示它,但 webClient 显示此错误:
org.springframework.web.reactive.function.client.UnknownHttpStatusCodeException: Unknown status code [432]
at org.springframework.web.reactive.function.client.DefaultClientResponse.lambda$createException(DefaultClientResponse.java:220) ~[spring-webflux-5.2.1.RELEASE.jar:5.2.1.RELEASE]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ 432 from POST https://apiThatIConnectTo....
如何避免这种情况并正常响应?并且可以将此状态代码添加到我的 JsonNode.class 或映射响应和状态代码或任何想法的自定义通用 class?谢谢
来自java doc。 (这也适用于 onRawStatus
方法)
To suppress the treatment of a status code as an error and process it as a normal response, return Mono.empty() from the function. The response will then propagate downstream to be processed.
示例代码片段如下所示
webClient.get()
.uri("http://someHost/somePath")
.retrieve()
.onRawStatus(status -> status == 432, response -> Mono.empty())
.bodyToMono(String.class);
我有一个管理项目所有请求的方法:
private Mono<JsonNode> postInternal(String service, String codPayment, String docNumber, RequestHeadersSpec<?> requestWeb) {
return requestWeb.retrieve().onStatus(HttpStatus::is4xxClientError,
clientResponse -> clientResponse.bodyToMono(ErrorClient.class).flatMap(
errorClient -> clientError(service, codPayment, clientResponse.statusCode(), docNumber, errorClient)))
.onStatus(HttpStatus::is5xxServerError,
clientResponse -> clientResponse.bodyToMono(ErrorClient.class)
.flatMap(errorClient -> serverError(service, codPayment, docNumber, errorClient)))
.onRawStatus(value -> value > 504 && value < 600,
clientResponse -> clientResponse.bodyToMono(ErrorClient.class)
.flatMap(errorClient -> otherError(service, codPayment, docNumber, errorClient)))
.bodyToMono(JsonNode.class);
}
但是我使用状态代码 432 响应的 API 之一,当响应正常但其中有一个特殊条件时,我必须显示它,但 webClient 显示此错误:
org.springframework.web.reactive.function.client.UnknownHttpStatusCodeException: Unknown status code [432]
at org.springframework.web.reactive.function.client.DefaultClientResponse.lambda$createException(DefaultClientResponse.java:220) ~[spring-webflux-5.2.1.RELEASE.jar:5.2.1.RELEASE]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ 432 from POST https://apiThatIConnectTo....
如何避免这种情况并正常响应?并且可以将此状态代码添加到我的 JsonNode.class 或映射响应和状态代码或任何想法的自定义通用 class?谢谢
来自java doc。 (这也适用于 onRawStatus
方法)
To suppress the treatment of a status code as an error and process it as a normal response, return Mono.empty() from the function. The response will then propagate downstream to be processed.
示例代码片段如下所示
webClient.get()
.uri("http://someHost/somePath")
.retrieve()
.onRawStatus(status -> status == 432, response -> Mono.empty())
.bodyToMono(String.class);