Mar*_*nyi 9

随着Spring Framework 5.3和Spring Boot 2.4的发布,Apache HttpClient 5.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();
Run Code Online (Sandbox Code Playgroud)

更新(基于@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>
Run Code Online (Sandbox Code Playgroud)

  • 我想提一下,您必须添加一些 Maven 依赖项才能打开此功能:`httpclient5`、`httpcore5-parent`、`httpcore5-reactive` (2认同)

小智 0

使用 org.apache.http.client.HttpClient 很难,因为它不是为它设计的,你可以做到这一点,但这将是一个安静的、不稳定的解决方案,需要你自己编写大量代码。最好使用像 HttpAsyncClient 这样设计的东西(顺便说一句,也来自 apache)。

在这里您可以找到一些信息和代码示例: https: //hc.apache.org/httpcomponents-asyncclient-ga/quickstart.html

祝你好运