[Fixed] How come Spring Webclient onStatus is not applicable for HttpStatus::is4xxClientError? – Spring

by
Ali Hasan
http-error spring spring-webclient spring-webflux undefined

The Solutions:

Solution 1: Predicate Type mismatch

In the provided code, the `onStatus` method expects a `Predicate` as its first argument, but `HttpStatus::is4xxClientError` is a static method that takes a `HttpStatusCode` as its argument. To resolve this issue, change the method reference to `HttpStatusCode::is4xxClientError` instead of `HttpStatus::is4xxClientError`:

// send getMap WMS to geoserver
public Mono<byte[]> getMap(String requestURL) {

    return geoserverWebClient
            .get()
            .uri(requestURL)
            .retrieve()
            .onStatus(HttpStatusCode::is4xxClientError,
                    error -> Mono.error(new RuntimeException("API not found")))
            .bodyToMono(byte[].class);
}

Q&A

Why did the Java Code have an error message “The method onStatus(Predicate, Function<ClientResponse,Mono<? extends Throwable>>) in the type WebClient.ResponseSpec is not applicable for the arguments (HttpStatus::is4xxClientError”

The type parameter in the Predicate (first argument to onStatus(...)) is not of type org.springframework.http.HttpStatus, but of type org.springframework.http.HttpStatusCode (which happens to be an interface that the HttpStatus enum implements).

How to fix Spring Webclient error message: “The method onStatus(Predicate, Function<ClientResponse,Mono<? extends Throwable>>) in the type WebClient.ResponseSpec is not applicable for the arguments (HttpStatus::is4xxClientError”

Switch the method reference from HttpStatus::is4xxClientError to HttpStatusCode::is4xxClientError