HttpComponentsClientHttpConnector 不接受 org.apache.http.impl.nio.client.CloseableHttpAsyncClient for Webclient with Apache Http Client

问题描述

我正在尝试在 Tomcat 上运行 Webflux,并尝试使用 Apache Http Client 创建 Sping WebClient。

参考文档说明有内置支持https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-client-builder-http-components

private ClientHttpConnector getApacheHttpClient(){
    HttpAsyncclientBuilder clientBuilder = HttpAsyncclients.custom();
    clientBuilder.setDefaultRequestConfig(RequestConfig.DEFAULT);
    CloseableHttpAsyncclient client = clientBuilder.build();
    ClientHttpConnector connector = new HttpComponentsClientHttpConnector(client);
    return connector;
}

但是 Springs HttpComponentsClientHttpConnector 不接受 org.apache.http.impl.nio.client.CloseableHttpAsyncclient。它需要 org.apache.hc.client5.http.impl.async.CloseableHttpAsyncclient。所以似乎有一个重命名,我找不到具有所需类的 Maven 依赖项。 有没有人知道该类的正确 Maven 依赖项。或者我怎样才能让它工作?

解决方法

Apache HTTP 客户端 5 是一个单独的工件。您需要将以下依赖项添加到您的 pom.xml

<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.1</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents.core5</groupId>
    <artifactId>httpcore5-reactive</artifactId>
    <version>5.1</version>
</dependency>
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.springframework.http.client.reactive.HttpComponentsClientHttpConnector;

public class ApacheHttp {
    public static void main(String[] args) {
        new HttpComponentsClientHttpConnector(HttpAsyncClients.custom().build())
    }
}