WebClient是從Spring WebFlux 5.0版本開始提供的一個非阻塞的基於響應式編程的進行Http請求的客戶端工具。它的響應式編程的基於Reactor的。WebClient中提供了標准Http請求方式對應的get、post、put、delete等方法,可以用來發起相應的請求。
增加pom引用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
簡單例子
下面的代碼是一個簡單的WebClient請求示例。可以通過WebClient.create()創建一個WebClient的實例,之后可以通過get()、post()等選擇調用方式,uri()指定需要請求的路徑,retrieve()用來發起請求並獲得響應,bodyToMono(String.class)用來指定請求結果需要處理為String,並包裝為Reactor的Mono對象。
WebClient webClient = WebClient.create(); Mono<String> mono = webClient.get().uri("https://www.baidu.com").retrieve().bodyToMono(String.class); System.out.println(mono.block());
URL中使用路徑變量
URL中也可以使用路徑變量,路徑變量的值可以通過uri方法的第2個參數指定。下面的代碼中就定義了URL中擁有一個路徑變量id,然后實際訪問時該變量將取值1。
webClient.get().uri("http://localhost:8081/user/{id}", 1);
URL中也可以使用多個路徑變量,多個路徑變量的賦值將依次使用uri方法的第2個、第3個、第N個參數。下面的代碼中就定義了URL中擁有路徑變量p1和p2,實際訪問的時候將被替換為var1和var2。所以實際訪問的URL是http://localhost:8081/user/var1/var2
。
webClient.get().uri("http://localhost:8081/user/{p1}/{p2}", "var1", "var2");
使用的路徑變量也可以通過Map進行賦值。面的代碼中就定義了URL中擁有路徑變量p1和p2,實際訪問的時候會從uriVariables中獲取值進行替換。所以實際訪問的URL是http://localhost:8081/user/var1/1
Map<String, Object> uriVariables = new HashMap<>(); uriVariables.put("p1", "var1"); uriVariables.put("p2", 1); webClient.get().uri("http://localhost:8081/user/{p1}/{p2}", uriVariables);
使用uriBuilder傳遞參數
String baseUrl = "http://192.1681.5.9:8989";
WebClient webClient = WebClient.create(baseUrl);
WebClient.RequestBodyUriSpec request = webClient.method(HttpMethod.POST);
request.uri(uriBuilder -> uriBuilder
.scheme("http")
.host("192.168.5.9")
.path("/mxtest4")
.port(8989)
.path("/mxtest4")
.queryParam("name1", "啊")
.queryParam("name2", "是")
.build());
指定baseUrl
在應用中使用WebClient時也許你要訪問的URL都來自同一個應用,只是對應不同的URL地址,這個時候可以把公用的部分抽出來定義為baseUrl,然后在進行WebClient請求的時候只指定相對於baseUrl的URL部分即可。這樣的好處是你的baseUrl需要變更的時候可以只要修改一處即可。下面的代碼在創建WebClient時定義了baseUrl為http://localhost:8081
,在發起Get請求時指定了URL為/user/1
,而實際上訪問的URL是http://localhost:8081/user/1
。
String baseUrl = "http://localhost:8081"; WebClient webClient = WebClient.create(baseUrl); Mono<User> mono = webClient.get().uri("user/{id}", 1).retrieve().bodyToMono(User.class);
Form提交
當傳遞的請求體對象是一個MultiValueMap對象時,WebClient默認發起的是Form提交。下面的代碼中就通過Form提交模擬了用戶進行登錄操作,給Form表單傳遞了參數username,值為u123,傳遞了參數password,值為p123。
String baseUrl = "http://localhost:8081"; WebClient webClient = WebClient.create(baseUrl); MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); map.add("username", "u123"); map.add("password", "p123"); Mono<String> mono = webClient.post().uri("/login").syncBody(map).retrieve().bodyToMono(String.class);
請求JSON
假設現在擁有一個新增User的接口,按照接口定義客戶端應該傳遞一個JSON對象,格式如下:
{ "name":"張三", "username":"zhangsan" }
客戶端可以建立一個滿足需要的JSON格式的對象,然后直接把該對象作為請求體,WebClient會幫我們自動把它轉換為JSON對象。
String baseUrl = "http://localhost:8081"; WebClient webClient = WebClient.create(baseUrl); User user = new User(); user.setName("張三"); user.setUsername("zhangsan"); Mono<Void> mono = webClient.post().uri("/user/add").syncBody(user).retrieve().bodyToMono(Void.class); mono.block();
如果沒有建立對應的對象,直接包裝為一個Map對象也是可以的,比如下面這樣。
String baseUrl = "http://localhost:8081"; WebClient webClient = WebClient.create(baseUrl); Map<String, Object> user = new HashMap<>(); user.put("name", "張三"); user.put("username", "zhangsan"); Mono<Void> mono = webClient.post().uri("/user/add").syncBody(user).retrieve().bodyToMono(Void.class); mono.block();
直接傳遞一個JSON字符串也是可以的,但是此時需要指定contentType為application/json
,也可以加上charset。默認情況下WebClient將根據傳遞的對象在進行解析處理后自動選擇ContentType。直接傳遞字符串時默認使用的ContentType會是text/plain
。其它情況下也可以主動指定ContentType。
String baseUrl = "http://localhost:8081"; WebClient webClient = WebClient.create(baseUrl); String userJson = "{" + " \"name\":\"張三\",\r\n" + " \"username\":\"zhangsan\"\r\n" + "}"; Mono<Void> mono = webClient.post().uri("/user/add").contentType(MediaType.APPLICATION_JSON_UTF8).syncBody(userJson).retrieve().bodyToMono(Void.class); mono.block();
處理WebClient錯誤
WebClient.ResponseSpec retrieve = request.retrieve();
Mono<String> mono = retrieve
.onStatus(e -> e.is4xxClientError(), resp -> {
System.out.println(resp.statusCode().value() + "," + resp.statusCode().getReasonPhrase());
return Mono.error(new RuntimeException(resp.statusCode().value() + " : " + resp.statusCode().getReasonPhrase()));
})
.bodyToMono(String.class)
.doOnError(WebClientResponseException.class, err -> {
System.out.println(err.getRawStatusCode() + "," + err.getResponseBodyAsString());
throw new RuntimeException(err.getMessage());
})
.onErrorReturn("fallback");
System.out.println("result:" + mono.block());
上傳和下載文件
上傳
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); HttpEntity<ClassPathResource> entity = new HttpEntity<>(new ClassPathResource("parallel.png"), headers); MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); parts.add("file", entity); Mono<String> resp = WebClient.create().post() .uri("http://localhost:8080/upload") .contentType(MediaType.MULTIPART_FORM_DATA) .body(BodyInserters.fromMultipartData(parts)) .retrieve().bodyToMono(String.class); LOGGER.info("result:{}",resp.block());
下載圖片
Mono<Resource> resp = WebClient.create().get() .uri("http://www.toolip.gr/captcha?complexity=99&size=60&length=9") .accept(MediaType.IMAGE_PNG) .retrieve().bodyToMono(Resource.class); Resource resource = resp.block(); BufferedImage bufferedImage = ImageIO.read(resource.getInputStream()); ImageIO.write(bufferedImage, "png", new File("captcha.png"));
下載文件
Mono<ClientResponse> resp = WebClient.create().get() .uri("http://localhost:8080/file/download") .accept(MediaType.APPLICATION_OCTET_STREAM) .exchange(); ClientResponse response = resp.block(); String disposition = response.headers().asHttpHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION); String fileName = disposition.substring(disposition.indexOf("=")+1); Resource resource = response.bodyToMono(Resource.class).block(); File out = new File(fileName); FileUtils.copyInputStreamToFile(resource.getInputStream(),out); LOGGER.info(out.getAbsolutePath());
異步調用
Flux<String> flux = request.retrieve().bodyToFlux(String.class); Disposable subscribe = flux.subscribe(tweet -> { //如果jvm結束了,就不能顯示了 System.out.println(tweet.toString()); }); System.out.println("result:exit"); Thread.sleep(5000);
exchange
前面介紹的示例都是直接獲取到了響應的內容,可能你會想獲取到響應的頭信息、Cookie等。那就可以在通過WebClient請求時把調用retrieve()
改為調用exchange()
,這樣可以訪問到代表響應結果的org.springframework.web.reactive.function.client.ClientResponse
對象,通過它可以獲取響應的狀態碼、Cookie等。下面的代碼先是模擬用戶進行了一次表單的登錄操作,通過ClientResponse獲取到了登錄成功后的寫入Cookie的sessionId,然后繼續請求了用戶列表。在請求獲取用戶列表時傳遞了存儲了sessionId的Cookie。
String baseUrl = "http://localhost:8081"; WebClient webClient = WebClient.create(baseUrl); MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); map.add("username", "u123"); map.add("password", "p123"); Mono<ClientResponse> mono = webClient.post().uri("login").syncBody(map).exchange(); ClientResponse response = mono.block(); if (response.statusCode() == HttpStatus.OK) { Mono<Result> resultMono = response.bodyToMono(Result.class); resultMono.subscribe(result -> { if (result.isSuccess()) { ResponseCookie sidCookie = response.cookies().getFirst("sid"); Flux<User> userFlux = webClient.get().uri("users").cookie(sidCookie.getName(), sidCookie.getValue()).retrieve().bodyToFlux(User.class); userFlux.subscribe(System.out::println); } }); }
WebClient.Builder
除了可以通過WebClient.create()
創建WebClient對象外,還可以通過WebClient.builder()
創建一個WebClient.Builder
對象,再對Builder對象進行一些配置后調用其build()
創建WebClient對象。下面的代碼展示了其用法,配置了baseUrl和默認的cookie信息。
String baseUrl = "http://localhost:8081";
WebClient webClient = WebClient.builder().baseUrl(baseUrl).defaultCookie("cookieName", "cookieValue").build();
//使用WebClient構建器,可以自定義選項:包括過濾器、默認標題、cookie、客戶端連接器等 WebClient webClient = WebClient.builder() .baseUrl("https://api.github.com") .defaultHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.github.v3+json") .defaultHeader(HttpHeaders.USER_AGENT, "Spring 5 WebClient") .build()
Builder還可以通過clientConnector()
定義需要使用的ClientHttpConnector,默認將使用org.springframework.http.client.reactive.ReactorClientHttpConnector
,其底層是基於netty的,如果你使用的是Maven,需要確保你的pom.xml中定義了如下依賴。
<dependency> <groupId>io.projectreactor.ipc</groupId> <artifactId>reactor-netty</artifactId> <version>0.7.8.RELEASE</version> </dependency>
如果對默認的發送請求和處理響應結果的編解碼不滿意,還可以通過exchangeStrategies()定義使用的ExchangeStrategies。ExchangeStrategies中定義了用來編解碼的對象,其也有對應的build()方法方便我們來創建ExchangeStrategies對象。
WebClient也提供了Filter,對應於org.springframework.web.reactive.function.client.ExchangeFilterFunction接口,其接口方法定義如下。
Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next)
在進行攔截時可以攔截request,也可以攔截response。下面的代碼定義的Filter就攔截了request,給每個request都添加了一個名為header1的header,值為value1。它也攔截了response,response中也是添加了一個新的header信息。攔截response時,如果新的ClientResponse對象是通過ClientResponse.from(response)
創建的,新的response是不會包含舊的response的body的,如果需要可以通過ClientResponse.Builder
的body()
指定,其它諸如header、cookie、狀態碼是會包含的。
String baseUrl = "http://localhost:8081"; WebClient webClient = WebClient.builder().baseUrl(baseUrl).filter((request, next) -> { ClientRequest newRequest = ClientRequest.from(request).header("header1", "value1").build(); Mono<ClientResponse> responseMono = next.exchange(newRequest); return Mono.fromCallable(() -> { ClientResponse response = responseMono.block(); ClientResponse newResponse = ClientResponse.from(response).header("responseHeader1", "Value1").build(); return newResponse; }); }).build();
如果定義的Filter只期望對某個或某些request起作用,可以在Filter內部通過request的相關屬性進行攔截,比如cookie信息、header信息、請求的方式或請求的URL等。也可以通過ClientRequest.attribute(attrName)
獲取某個特定的屬性,該屬性是在請求時通過attribute("attrName", "attrValue")
指定的。這跟在HttpServletRequest中添加的屬性的作用范圍是類似的。
配置連接池,超時時間等
@Configuration public class WebClientConfig { @Bean ReactorResourceFactory resourceFactory() { ReactorResourceFactory factory = new ReactorResourceFactory(); factory.setUseGlobalResources(false); factory.setConnectionProvider(ConnectionProvider.fixed("httpClient", 50, 10)); factory.setLoopResources(LoopResources.create("httpClient", 50, true)); return factory; } @Bean WebClient webClient() { Function<HttpClient, HttpClient> mapper = client -> client.tcpConfiguration(c -> c.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10) .option(TCP_NODELAY, true) .doOnConnected(conn -> { conn.addHandlerLast(new ReadTimeoutHandler(10)); conn.addHandlerLast(new WriteTimeoutHandler(10)); })); ClientHttpConnector connector = new ReactorClientHttpConnector(resourceFactory(), mapper); return WebClient.builder().clientConnector(connector).build(); } }
參數
https://blog.csdn.net/iteye_13139/article/details/82726588
https://segmentfault.com/a/1190000012916413
https://juejin.im/post/5d6c9507e51d4561f777e20b