如何从APACHE Http客户端创建Spring WebClient

我想从HttpComponent的org.apache.http.client.HttpClient创建WebClient,以便在异步操作中使用它。有关如何操作的任何想法


解决方案

随着Spring Framework5.3和Spring Boot2.4的发布,现在在ApacheHttpClient5.0和Spring WebClient之间有内置集成。

HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
clientBuilder.setDefaultRequestConfig(...);
CloseableHttpAsyncClient client = clientBuilder.build();
ClientHttpConnector connector = new HttpComponentsClientHttpConnector(client);

WebClient webClient = WebClient.builder().clientConnector(connector).build();

更新(基于@kolyaiks的评论)

必需的依赖项:

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

相关文章