HttpComponentsClientHttpConnector is not accepting org.apache.http.impl.nio.client.CloseableHttpAsyncClient for Webclient with Apache Http Client

1.1k views Asked by At

Im trying to run Webflux on Tomcat and try to create Sping WebClient with Apache Http Client.

Reference Documentation stated that theres built-in support: 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;
}

But Springs HttpComponentsClientHttpConnector is not accepting org.apache.http.impl.nio.client.CloseableHttpAsyncClient. It requires org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient. So there seems to be a package rename and I can´t find a Maven Dependency that has the required class. Does anybody know the right Maven Dependency for that class. Or how could I make it work?

1

There are 1 answers

1
Martin Tarjányi On

Apache HTTP Client 5 is a separate artifact. You'll need to add the following dependencies to your 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())
    }
}