javascript
Spring的WebClient基本使用
WebClient是從Spring WebFlux 5.0版本開(kāi)始提供的一個(gè)非阻塞的基于響應(yīng)式編程的進(jìn)行Http請(qǐng)求的客戶端工具。它的響應(yīng)式編程的基于Reactor的。WebClient中提供了標(biāo)準(zhǔn)Http請(qǐng)求方式對(duì)應(yīng)的get、post、put、delete等方法,可以用來(lái)發(fā)起相應(yīng)的請(qǐng)求。
增加pom引用
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></dependency>簡(jiǎn)單例子
下面的代碼是一個(gè)簡(jiǎn)單的WebClient請(qǐng)求示例。可以通過(guò)WebClient.create()創(chuàng)建一個(gè)WebClient的實(shí)例,之后可以通過(guò)get()、post()等選擇調(diào)用方式,uri()指定需要請(qǐng)求的路徑,retrieve()用來(lái)發(fā)起請(qǐng)求并獲得響應(yīng),bodyToMono(String.class)用來(lái)指定請(qǐng)求結(jié)果需要處理為String,并包裝為Reactor的Mono對(duì)象。
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中也可以使用路徑變量,路徑變量的值可以通過(guò)uri方法的第2個(gè)參數(shù)指定。下面的代碼中就定義了URL中擁有一個(gè)路徑變量id,然后實(shí)際訪問(wèn)時(shí)該變量將取值1。
webClient.get().uri("http://localhost:8081/user/{id}", 1);URL中也可以使用多個(gè)路徑變量,多個(gè)路徑變量的賦值將依次使用uri方法的第2個(gè)、第3個(gè)、第N個(gè)參數(shù)。下面的代碼中就定義了URL中擁有路徑變量p1和p2,實(shí)際訪問(wèn)的時(shí)候?qū)⒈惶鎿Q為var1和var2。所以實(shí)際訪問(wèn)的URL是http://localhost:8081/user/var1/var2。
webClient.get().uri("http://localhost:8081/user/{p1}/{p2}", "var1", "var2");使用的路徑變量也可以通過(guò)Map進(jìn)行賦值。面的代碼中就定義了URL中擁有路徑變量p1和p2,實(shí)際訪問(wèn)的時(shí)候會(huì)從uriVariables中獲取值進(jìn)行替換。所以實(shí)際訪問(wèn)的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傳遞參數(shù)
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
在應(yīng)用中使用WebClient時(shí)也許你要訪問(wèn)的URL都來(lái)自同一個(gè)應(yīng)用,只是對(duì)應(yīng)不同的URL地址,這個(gè)時(shí)候可以把公用的部分抽出來(lái)定義為baseUrl,然后在進(jìn)行WebClient請(qǐng)求的時(shí)候只指定相對(duì)于baseUrl的URL部分即可。這樣的好處是你的baseUrl需要變更的時(shí)候可以只要修改一處即可。下面的代碼在創(chuàng)建WebClient時(shí)定義了baseUrl為http://localhost:8081,在發(fā)起Get請(qǐng)求時(shí)指定了URL為/user/1,而實(shí)際上訪問(wèn)的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提交
當(dāng)傳遞的請(qǐng)求體對(duì)象是一個(gè)MultiValueMap對(duì)象時(shí),WebClient默認(rèn)發(fā)起的是Form提交。下面的代碼中就通過(guò)Form提交模擬了用戶進(jìn)行登錄操作,給Form表單傳遞了參數(shù)username,值為u123,傳遞了參數(shù)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);請(qǐng)求JSON
假設(shè)現(xiàn)在擁有一個(gè)新增User的接口,按照接口定義客戶端應(yīng)該傳遞一個(gè)JSON對(duì)象,格式如下:
{"name":"張三","username":"zhangsan" }客戶端可以建立一個(gè)滿足需要的JSON格式的對(duì)象,然后直接把該對(duì)象作為請(qǐng)求體,WebClient會(huì)幫我們自動(dòng)把它轉(zhuǎn)換為JSON對(duì)象。
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();如果沒(méi)有建立對(duì)應(yīng)的對(duì)象,直接包裝為一個(gè)Map對(duì)象也是可以的,比如下面這樣。
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();直接傳遞一個(gè)JSON字符串也是可以的,但是此時(shí)需要指定contentType為application/json,也可以加上charset。默認(rèn)情況下WebClient將根據(jù)傳遞的對(duì)象在進(jìn)行解析處理后自動(dòng)選擇ContentType。直接傳遞字符串時(shí)默認(rèn)使用的ContentType會(huì)是text/plain。其它情況下也可以主動(dòng)指定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錯(cuò)誤
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());異步調(diào)用
Flux<String> flux = request.retrieve().bodyToFlux(String.class); Disposable subscribe = flux.subscribe(tweet -> {//如果jvm結(jié)束了,就不能顯示了System.out.println(tweet.toString()); }); System.out.println("result:exit"); Thread.sleep(5000);exchange
前面介紹的示例都是直接獲取到了響應(yīng)的內(nèi)容,可能你會(huì)想獲取到響應(yīng)的頭信息、Cookie等。那就可以在通過(guò)WebClient請(qǐng)求時(shí)把調(diào)用retrieve()改為調(diào)用exchange(),這樣可以訪問(wèn)到代表響應(yīng)結(jié)果的org.springframework.web.reactive.function.client.ClientResponse對(duì)象,通過(guò)它可以獲取響應(yīng)的狀態(tài)碼、Cookie等。下面的代碼先是模擬用戶進(jìn)行了一次表單的登錄操作,通過(guò)ClientResponse獲取到了登錄成功后的寫(xiě)入Cookie的sessionId,然后繼續(xù)請(qǐng)求了用戶列表。在請(qǐng)求獲取用戶列表時(shí)傳遞了存儲(chǔ)了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
除了可以通過(guò)WebClient.create()創(chuàng)建WebClient對(duì)象外,還可以通過(guò)WebClient.builder()創(chuàng)建一個(gè)WebClient.Builder對(duì)象,再對(duì)Builder對(duì)象進(jìn)行一些配置后調(diào)用其build()創(chuàng)建WebClient對(duì)象。下面的代碼展示了其用法,配置了baseUrl和默認(rèn)的cookie信息。
String baseUrl = "http://localhost:8081"; WebClient webClient = WebClient.builder().baseUrl(baseUrl).defaultCookie("cookieName", "cookieValue").build(); //使用WebClient構(gòu)建器,可以自定義選項(xiàng):包括過(guò)濾器、默認(rèn)標(biāo)題、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還可以通過(guò)clientConnector()定義需要使用的ClientHttpConnector,默認(rèn)將使用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>如果對(duì)默認(rèn)的發(fā)送請(qǐng)求和處理響應(yīng)結(jié)果的編解碼不滿意,還可以通過(guò)exchangeStrategies()定義使用的ExchangeStrategies。ExchangeStrategies中定義了用來(lái)編解碼的對(duì)象,其也有對(duì)應(yīng)的build()方法方便我們來(lái)創(chuàng)建ExchangeStrategies對(duì)象。
WebClient也提供了Filter,對(duì)應(yīng)于org.springframework.web.reactive.function.client.ExchangeFilterFunction接口,其接口方法定義如下。
Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next)在進(jìn)行攔截時(shí)可以攔截request,也可以攔截response。下面的代碼定義的Filter就攔截了request,給每個(gè)request都添加了一個(gè)名為header1的header,值為value1。它也攔截了response,response中也是添加了一個(gè)新的header信息。攔截response時(shí),如果新的ClientResponse對(duì)象是通過(guò)ClientResponse.from(response)創(chuàng)建的,新的response是不會(huì)包含舊的response的body的,如果需要可以通過(guò)ClientResponse.Builder的body()指定,其它諸如header、cookie、狀態(tài)碼是會(huì)包含的。
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只期望對(duì)某個(gè)或某些request起作用,可以在Filter內(nèi)部通過(guò)request的相關(guān)屬性進(jìn)行攔截,比如cookie信息、header信息、請(qǐng)求的方式或請(qǐng)求的URL等。也可以通過(guò)ClientRequest.attribute(attrName)獲取某個(gè)特定的屬性,該屬性是在請(qǐng)求時(shí)通過(guò)attribute("attrName", "attrValue")指定的。這跟在HttpServletRequest中添加的屬性的作用范圍是類似的。
配置連接池,超時(shí)時(shí)間等
@Configuration public class WebClientConfig {@BeanReactorResourceFactory resourceFactory() {ReactorResourceFactory factory = new ReactorResourceFactory();factory.setUseGlobalResources(false);factory.setConnectionProvider(ConnectionProvider.fixed("httpClient", 50, 10));factory.setLoopResources(LoopResources.create("httpClient", 50, true));return factory;}@BeanWebClient 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();} }?
參數(shù)
https://blog.csdn.net/iteye_13139/article/details/82726588
https://segmentfault.com/a/1190000012916413
https://juejin.im/post/5d6c9507e51d4561f777e20b
https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-client
?
總結(jié)
以上是生活随笔為你收集整理的Spring的WebClient基本使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MyBatis拦截器原理探究
- 下一篇: SpringBoot+Dubbo集成EL