Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Spring WebClient from Apache Http Client

I want to create WebClient from HttpComponent's org.apache.http.client.HttpClient to use it in async operations.Any idea on how to do it

like image 960
user3847425 Avatar asked Jun 21 '26 23:06

user3847425


1 Answers

With the release of Spring Framework 5.3 and Spring Boot 2.4 now there is built-in integration between Apache HttpClient 5.0 and Spring WebClient.

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

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

UPDATE (based on @kolyaiks's comment)

Necessary dependencies:

<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>
like image 148
Martin Tarjányi Avatar answered Jun 23 '26 17:06

Martin Tarjányi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!