简体   繁体   English

设置 WebClient 以使用 HttpComponents

[英]Setup WebClient to use HttpComponents

this works (ie default WebClient)这有效(即默认 WebClient)

WebClient webClient = WebClient.create(url);
Mono<String> mono = webClient.get().exchangeToMono(clientResponse -> {
Mono<String> monos = clientResponse.bodyToMono(String.class);
     return monos;
     });
Mono<ServerResponse> response = ServerResponse.ok().body(mono, String.class);

and when replacing the first line with并将第一行替换为

HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom();
    CloseableHttpAsyncClient closeableHttpAsyncClient = httpAsyncClientBuilder.build();
    ClientHttpConnector clientHttpConnector = new HttpComponentsClientHttpConnector(closeableHttpAsyncClient);
    WebClient webClient = WebClient.builder().baseUrl(url).clientConnector(clientHttpConnector).build();

to use async HttpComponents.使用异步 HttpComponents。 This results in ERR_EMPTY_RESPONSE with the client not sending the request to the server URL. I couldn't find a minimally complete working example of the WebClient using HttpComponents setup.这导致 ERR_EMPTY_RESPONSE,客户端未将请求发送到服务器 URL。我找不到使用 HttpComponents 设置的 WebClient 的最低限度完整工作示例。 What am I missing here?我在这里错过了什么?

The pom info - spring-boot-starter-parent 3.0.1, httpclient5 and httpcore5 5.2 pom 信息 - spring-boot-starter-parent 3.0.1、httpclient5 和 httpcore5 5.2

Not sure how did you tests the above code but here is a working test using HttpAsyncClient .不确定你是如何测试上面的代码的,但这是一个使用HttpAsyncClient的工作测试。

@Test
void test() {
    CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.custom().build();
    ClientHttpConnector clientHttpConnector = new HttpComponentsClientHttpConnector(httpAsyncClient);
    WebClient webClient = WebClient.builder()
            .baseUrl("https://google.com")
            .clientConnector(clientHttpConnector)
            .build();

    Mono<String> request = webClient.get()
            .uri("/")
            .retrieve()
            .bodyToMono(String.class);

    StepVerifier.create(request)
            .assertNext(res -> log.info("response: {}", res))
            .verifyComplete();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM