【问题标题】:How add or Ignoring a specific http status code in WebClient reactor?如何在 WebClient 反应器中添加或忽略特定的 http 状态代码?
【发布时间】:2021-02-22 17:57:32
【问题描述】:

我有一个方法来管理我项目的所有请求:

 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);
}

但是当响应正常但其中有特殊条件并且我必须显示它但 webClient 显示此错误时,我使用状态代码 432 响应的 API 之一:

org.springframework.web.reactive.function.client.UnknownHttpStatusCodeException: Unknown status code [432]
at org.springframework.web.reactive.function.client.DefaultClientResponse.lambda$createException$1(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 或映射响应和状态代码的自定义通用类或任何想法?谢谢

【问题讨论】:

  • 通过使用webclient#exchange 并检查响应中的状态代码
  • @Toerktumlare 你能给我看一个示例代码吗?我是 WebFlux 的新手:/
  • 如果你是新手,那么你应该谷歌,阅读webclient上的官方文档并实际学习框架docs.spring.io/spring-framework/docs/current/reference/html/…
  • @Toerktumlare 我读到了,否则我不会创建该方法,但我的问题是 HttpStatus 代码不是“org.springframework.http.HttpStatus”的一部分,是自定义代码和“。 onStatus" 将所有 4xx 和 5xx 状态代码都视为错误,但对我来说 432 不是错误代码,因为我需要帮助就是...

标签: java spring spring-webflux spring-webclient


【解决方案1】:

来自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.

示例代码 sn-p 看起来像

    webClient.get()
            .uri("http://someHost/somePath")
            .retrieve()
            .onRawStatus(status -> status == 432, response -> Mono.empty())
            .bodyToMono(String.class);

【讨论】:

  • 谢谢,我已经将相同的条件放入 previus onRawStatus 但不起作用我不知道为什么,但我只是像这样放置其他 onRawStatus:......onRawStatus(value -> value == 432, clientResponse -> Mono.empty()) .onRawStatus(value -> value > 504 && value clientResponse.bodyToMono(ErrorClient.class) .flatMap(errorClient -> otherError(service, codPayment, docNumber, errorClient))) .bodyToMono(JsonNode.class) 并且工作正常。 xD谢谢
猜你喜欢
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 2019-11-27
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
  • 2020-02-20
相关资源
最近更新 更多