Spring的WebClient使用

WebClient使用

引言

Spring Framework 5 包括一个新的 spring-webflux 模块。该模块包含对响应式 HTTP 和 WebSocket 客户端的支持,以及对REST,HTML浏览器和 WebSocket风格交互的响应式服务器Web应用程序的支持。本文主要介绍WebClient的使用,包括通过WebClient请求接口及实现接口的文件上传下载。

1.引入依赖

在pom.xml中引入WebClient所需的依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
    <groupId>org.projectreactor</groupId>
    <artifactId>reactor-spring</artifactId>
    <version>1.0.1.RELEASE</version>
</dependency>

2.一个简单的请求示例

请求百度首页示例

// 1.创建WebClient实例
WebClient webClient = WebClient
                .builder()
                .baseUrl("https://www.baidu.com")
                .build();

// 2.通过通过retrieve()请求并获得响应
Mono<String> bodyToMono = webClient
                .get()
                .retrieve()
                // 将请求结果处理为String类型
                .bodyToMono(String.class);

log.info("请求返回的数据内容:{}", bodyToMono.block());

3.WebClient配置

在创建WebClient时可对一些变量进行默认设置

WebClient client3 = WebClient
  .builder()
    .baseUrl("http://localhost:8080")
    .defaultCookie("cookieKey", "cookieValue")
    .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) 
    .defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080"))
  .build();

也可在发起请求时填入cookie、header等参数

webClient
  .get()
  .uri("https://www.baidu.com")
  .cookie("cookieKey", "cookieValue")
  .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
  .retrieve();

4.在请求中传参

在日常使用中传递参数的方法

WebClient webClient = WebClient.create();
Mono<String> bodyToMono = webClient.post()
  .uri(uriBuilder ->
       uriBuilder
       .scheme("https")
       .host("localhost:8080")
       .path("/api/test")
       .queryParam("id", "123")
       .queryParam("invalid", "invalid123")
       .build()
      )
  // requestbody,也可以使用MultiValueMap<String, String> map = new LinkedMultiValueMap<>(),传入参数,发起form提交。或直接使用HashMap传参
  .syncBody(new Student())
  .retrieve()
  .bodyToMono(String.class);

5.设置连接超时时间

设置请求超时时间

TcpClient tcpClient = TcpClient
              .create()
              .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
              .doOnConnected(connection -> {
	connection.addHandlerLast(new ReadTimeoutHandler(5000, TimeUnit.MILLISECONDS));
	connection.addHandlerLast(new WriteTimeoutHandler(5000, TimeUnit.MILLISECONDS));
                });

WebClient client = WebClient.builder()
                .clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
                .build();

6.文件上传

上传文件代码

HttpHeaders headers = new HttpHeaders();
headers.add("content-type", "application/x-www-form-urlencoded");
HttpEntity<ClassPathResource> entity = new HttpEntity<>(new ClassPathResource("a.txt"), headers);
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("file", entity);
Mono<String> bodyToMono = WebClient.create().post()
        .uri("http://localhost:8080/upload")
        .contentType(MediaType.MULTIPART_FORM_DATA)
        .body(BodyInserters.fromMultipartData(parts))
        .retrieve().bodyToMono(String.class);

7.文件下载

通过将.retrieve()方法换成.exchange(),可以获取到响应的头信息、Cookie等信息,与restTemplate相似。

Mono<ClientResponse> responseMono = WebClient.create().get()
                .uri("http://localhost:8080/file/download")
                .accept(MediaType.APPLICATION_OCTET_STREAM)
                .exchange(); 
ClientResponse response = responseMono.block();
// 可从headers中获取filename等信息 
ClientResponse.Headers headers = response.headers();
Resource resource = response.bodyToMono(Resource.class).block();
// 获取文件流
InputStream inputStream = resource.getInputStream();

8.WebTestClient

The WebTestClient is the main entry point for testing WebFlux server endpoints. It has a very similar API to the WebClient, and it delegates most of the work to an internal WebClient instance focusing mainly on providing a test context. The DefaultWebTestClient class is a
single interface implementation.

主要意思是WebTestClient用于测试WebFlux服务器端点,他和WebClient相似,且将大部分工作委托给了内部的WebClient,主要用于测试上下文,此部分后期在单独整理。

下面是一个使用Spring WebClient进行异步POST请求的代码范例,带有注释解释每个步骤的作用: ```java import org.springframework.http.MediaType; import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; import java.time.Duration; public class WebClientExample { public static void main(String[] args) { // 创建一个WebClient实例 WebClient webClient = WebClient.builder() // 指定请求超时时间 .clientConnector(new ReactorClientHttpConnector()) .defaultHeader("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE) .defaultHeader("Accept", MediaType.APPLICATION_JSON_VALUE) .baseUrl("http://localhost:8080") .build(); // 构造请求参数 MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); formData.add("param1", "value1"); formData.add("param2", "value2"); // 发送POST请求 Mono<ClientResponse> responseMono = webClient.post() // 指定请求路径 .uri("/api/path") // 设置请求体 .body(BodyInserters.fromFormData(formData)) // 发送请求并返回响应结果 .exchange(); // 处理响应结果 responseMono.subscribe(response -> { // 打印响应状态码 System.out.println(response.statusCode()); // 打印响应头 response.headers().asHttpHeaders().forEach((name, values) -> { System.out.println(name + ": " + values); }); // 打印响应体 response.bodyToMono(String.class).subscribe(System.out::println); }); } } ``` 在这个例子中,我们创建了一个WebClient实例,并指定了请求超时时间、默认请求头、请求的基础URL等信息。然后,我们构造了一个包含请求参数的MultiValueMap对象,并将其作为请求体发送POST请求。 WebClient的post()方法返回一个RequestHeadersSpec对象,该对象可以用于设置请求头、请求体等信息。在这个例子中,我们使用uri()方法指定了请求路径,并使用body()方法设置了请求体。 最后,我们调用exchange()方法发送请求并返回响应结果。exchange()方法返回一个Mono<ClientResponse>对象,我们可以使用subscribe()方法对其进行订阅,然后在回调函数中处理响应结果。在这个例子中,我们打印了响应状态码、响应头和响应体。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值