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
Asked
Active
Viewed 7,850 times
6

Martin Tarjányi
- 8,863
- 2
- 31
- 49

user3847425
- 125
- 1
- 11
-
1I don't even have an idea what exactly you're trying to achieve. What did you do so far? Where did run into problems? – Thomas Aug 09 '18 at 11:25
-
Please, provide more details – Sergey Prosin Aug 09 '18 at 11:55
-
Hi Thomas Hi Sergey, I basically have the http client object and need to create a web client object to make async calls as that is what is only supported for reactive programming .Unable to find a way to do that – user3847425 Aug 09 '18 at 14:34
-
Spring currently supports Jetty and Netty clients. See [How to use Jetty client](https://stackoverflow.com/questions/47018963/). Basically any implementation of [ClientHttpConnector](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/client/reactive/ClientHttpConnector.html) will work. At this time httpcomponent's implementation is not available - it might be added in future. – Ritesh Dec 14 '19 at 14:41
-
Work on using [HttpClient of Java 11](https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html) is in progress. See [JDK 11 HttpClient integration with WebClient](https://github.com/spring-projects/spring-framework/issues/21014). – Ritesh Dec 14 '19 at 14:52
2 Answers
8
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>

Martin Tarjányi
- 8,863
- 2
- 31
- 49
-
2I'd like to mention that you have to add some Maven dependencies to turn this on: `httpclient5`, `httpcore5-parent`, `httpcore5-reactive` – kolyaiks May 15 '21 at 22:36
0
With the org.apache.http.client.HttpClient it's hard cause it was not designed for it you could do it but that would be a quiet scatchy solution with a lot of coding yourself. Better use something designed for like the HttpAsyncClient (also from apache btw.)
Here you find some information and a code example: https://hc.apache.org/httpcomponents-asyncclient-ga/quickstart.html
Good luck

LeBraveLittleToaster
- 177
- 1
- 13
-
Can you please share what could be the dirty solution if we dont have the option to use the HttpAsyncClient as we only have the HTTPClient object available whereas we need to use the Spring Webflux based WebClient to make rest calls. – user3847425 Aug 09 '18 at 14:33