-1

I have a this method that manage all request of my project:

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

But one of the API that i consume response with Status code 432 when the response is ok but have a special condition in it and i must show it but webClient show this error:

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

How can i avoid this and response normally? and is possible to add this status code to my JsonNode.class or a custom generic class that map the response and the status code or any ideas? thanks

Juan Sanchez
  • 113
  • 1
  • 9
  • By using `webclient#exchange` and check status codes in the response – Toerktumlare Feb 22 '21 at 18:24
  • @Toerktumlare can you show me a example code? i am new with WebFlux :/ – Juan Sanchez Feb 22 '21 at 18:29
  • If you are new then you should google, read the official documentation on webclient and actually learn the framework https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-client-exchange – Toerktumlare Feb 22 '21 at 18:43
  • @Toerktumlare i read that or else i will not created that method but my problem is that the HttpStatus code is Not part of the "org.springframework.http.HttpStatus" is a custom one and ".onStatus" treat like a error all 4xx and 5xx status code but for me 432 is NOT a error code, because this i need help is all... – Juan Sanchez Feb 22 '21 at 19:06

1 Answers1

1

From the java doc. (This also applies to the onRawStatus method)

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.

An example code snippet would look like

    webClient.get()
            .uri("http://someHost/somePath")
            .retrieve()
            .onRawStatus(status -> status == 432, response -> Mono.empty())
            .bodyToMono(String.class);
Michael McFadyen
  • 2,675
  • 11
  • 25
  • Thanks i have been putting into the previus onRawStatus that same condition but not work i dont know why but i simply put other onRawStatus like this: ......onRawStatus(value -> value == 432, clientResponse -> Mono.empty()) .onRawStatus(value -> value > 504 && value < 600, clientResponse -> clientResponse.bodyToMono(ErrorClient.class) .flatMap(errorClient -> otherError(service, codPayment, docNumber, errorClient))) .bodyToMono(JsonNode.class) and that work fine. xD THANKS – Juan Sanchez Feb 22 '21 at 20:41