使用 Spring Webclient 产生“HttpsStatus 没有定义 is4xxClientError”

问题描述 投票:0回答:0

我已经实现了一个基于

spring-boot-starter-webflux
神器的网络客户端。

代码:

// create  client bean to use throughout services
@Bean
public WebClient geoserverWebClient() {
    // to not fall prone to DataBufferLimitException
    final int size = 16 * 1024 * 1024;
    final ExchangeStrategies sΩtrategies = ExchangeStrategies.builder()
        .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
        .build();
    
    return WebClient.builder()
        .exchangeStrategies(strategies)
        .baseUrl(geoserverURL) 
        .defaultHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString(geoserverBasicAuth.getBytes()))
        .build();
}

到目前为止,我在使用它时没有遇到任何问题,例如:

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

    return geoserverWebClient
    .get()
    .uri(requestURL)
    .retrieve()
    .bodyToMono(byte[].class);
}

但是,如果我向其中添加

onStatus
方法以检查 HTTP 错误,我会收到错误消息:“HttpStatus 类型未定义 is4xxClientError(HttpStatusCode)”

// various other imports ...

import org.springframework.http.HttpStatus;

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

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

我已经阅读了该方法,因为我使用的是 Spring Boot 版本 3.0.5,所以我不明白为什么它不能像记录的那样工作。

我尝试从

Builder
开始,但它不会改变一点。我还尝试了其他 HTTP 错误,甚至写出了
org.springframework.http.HttpStatus::is4xxClientError
。这些都没有用。我唯一没有做的是实例化一个
HttpStatus
对象 - 但我假设
.onStatus()
方法会从
.retrieve()
方法中获得正确的对象。

我认为也许我的 IDE 目前没有正确加载导入。我已经关闭项目,重新启动IDE并重新打开项目但无济于事。

spring undefined spring-webflux spring-webclient http-error
© www.soinside.com 2019 - 2024. All rights reserved.