WebClient is a non blocking and responsive Http client provided by Spring. It provides synchronous and asynchronous API s, which will replace RestTemplate and AsyncRestTemplate. The software versions used in this paper are Java 1.8.0, SpringBoot 2.2.1.RELEASE.
1. Server
See also Java calls Http interface (1) -- write server
2. Call
The use of WebClient requires the use of Reactor Netty, which depends on the following:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webflux</artifactId> </dependency> <dependency> <groupId>io.projectreactor.netty</groupId> <artifactId>reactor-netty</artifactId> </dependency>
2.1 GET request
public static void get() { String requestPath = "http://Localhost: 8080 / demo / httptest / getuser? Userid = 1000 & username = Li Bai“; WebClient webClient = WebClient.create(); Mono<String> mono = webClient.get().uri(requestPath).retrieve().bodyToMono(String.class); //Synchronization mode System.out.println("get block Return result:" + mono.block()); //Asynchronous mode final CountDownLatch latch = new CountDownLatch(5); for (int i = 0; i < 5; i++) { requestPath = "http://Localhost: 8080 / demo / httptest / getuser? Userid = 1000 & username = Li Bai "+ i; mono = webClient.get().uri(requestPath).retrieve().bodyToMono(String.class); mono.subscribe(new Consumer<String>() { @Override public void accept(String s) { latch.countDown(); System.out.println("get subscribe Return result:" + s); } }); } try { latch.await(); } catch (Exception e) { e.printStackTrace(); } }
2.2 POST request (send key value pair data)
public static void post() { String requestPath = "http://localhost:8080/demo/httptest/getUser"; WebClient webClient = WebClient.create(); MultiValueMap<String, String> map = new LinkedMultiValueMap <String, String>(); map.add("userId", "1000"); map.add("userName", "Li Bai"); Mono<String> mono = webClient.post().uri(requestPath).bodyValue(map).retrieve().bodyToMono(String.class); System.out.println("post Return result:" + mono.block()); }
2.3 POST request (send JSON data)
public static void post2() { String requestPath = "http://localhost:8080/demo/httptest/addUser"; WebClient webClient = WebClient.create(); String param = "{\"userId\": \"1001\",\"userName\":\"Du Fu\"}"; Mono<String> mono = webClient.post().uri(requestPath).contentType(MediaType.APPLICATION_JSON).bodyValue(param) .retrieve().bodyToMono(String.class); System.out.println("post json Return result:" + mono.block()); }
2.4 upload files
public static void upload() { String requestPath = "http://localhost:8080/demo/httptest/upload"; WebClient webClient = WebClient.create(); Mono<String> mono = webClient.post().uri(requestPath).contentType(MediaType.APPLICATION_OCTET_STREAM) .bodyValue(new FileSystemResource("d:/a.jpg")).retrieve().bodyToMono(String.class); System.out.println("upload Return result:" + mono.block()); }
2.5 upload file and send key value pair data
public static void mulit() { String requestPath = "http://localhost:8080/demo/httptest/multi"; WebClient webClient = WebClient.create(); MultipartBodyBuilder builder = new MultipartBodyBuilder(); builder.part("param1", "Parameter 1"); builder.part("param2", "Parameter 2"); builder.part("file", new FileSystemResource("d:/a.jpg")); MultiValueMap<String, HttpEntity<?>> parts = builder.build(); Mono<String> mono = webClient.post().uri(requestPath) .bodyValue(parts).retrieve().bodyToMono(String.class); System.out.println("mulit Return result:" + mono.block()); }
2.6 complete example
package com.inspur.demo.http.client; import java.util.concurrent.CountDownLatch; import java.util.function.Consumer; import org.springframework.core.io.FileSystemResource; import org.springframework.http.HttpEntity; import org.springframework.http.MediaType; import org.springframework.http.client.MultipartBodyBuilder; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; /** * * Calling Http interface through WebClient * */ public class WebClientCase { /** * GET request */ public static void get() { String requestPath = "http://Localhost: 8080 / demo / httptest / getuser? Userid = 1000 & username = Li Bai“; WebClient webClient = WebClient.create(); Mono<String> mono = webClient.get().uri(requestPath).retrieve().bodyToMono(String.class); //Synchronization mode System.out.println("get block Return result:" + mono.block()); //Asynchronous mode final CountDownLatch latch = new CountDownLatch(5); for (int i = 0; i < 5; i++) { requestPath = "http://Localhost: 8080 / demo / httptest / getuser? Userid = 1000 & username = Li Bai "+ i; mono = webClient.get().uri(requestPath).retrieve().bodyToMono(String.class); mono.subscribe(new Consumer<String>() { @Override public void accept(String s) { latch.countDown(); System.out.println("get subscribe Return result:" + s); } }); } try { latch.await(); } catch (Exception e) { e.printStackTrace(); } } /** * POST Request (send key value pair data) */ public static void post() { String requestPath = "http://localhost:8080/demo/httptest/getUser"; WebClient webClient = WebClient.create(); MultiValueMap<String, String> map = new LinkedMultiValueMap <String, String>(); map.add("userId", "1000"); map.add("userName", "Li Bai"); Mono<String> mono = webClient.post().uri(requestPath).bodyValue(map).retrieve().bodyToMono(String.class); System.out.println("post Return result:" + mono.block()); } /** * POST Request (send json data) */ public static void post2() { String requestPath = "http://localhost:8080/demo/httptest/addUser"; WebClient webClient = WebClient.create(); String param = "{\"userId\": \"1001\",\"userName\":\"Du Fu\"}"; Mono<String> mono = webClient.post().uri(requestPath).contentType(MediaType.APPLICATION_JSON).bodyValue(param) .retrieve().bodyToMono(String.class); System.out.println("post json Return result:" + mono.block()); } /** * Upload files */ public static void upload() { String requestPath = "http://localhost:8080/demo/httptest/upload"; WebClient webClient = WebClient.create(); Mono<String> mono = webClient.post().uri(requestPath).contentType(MediaType.APPLICATION_OCTET_STREAM) .bodyValue(new FileSystemResource("d:/a.jpg")).retrieve().bodyToMono(String.class); System.out.println("upload Return result:" + mono.block()); } /** * Upload file and send key value pair data */ public static void mulit() { String requestPath = "http://localhost:8080/demo/httptest/multi"; WebClient webClient = WebClient.create(); MultipartBodyBuilder builder = new MultipartBodyBuilder(); builder.part("param1", "Parameter 1"); builder.part("param2", "Parameter 2"); builder.part("file", new FileSystemResource("d:/a.jpg")); MultiValueMap<String, HttpEntity<?>> parts = builder.build(); Mono<String> mono = webClient.post().uri(requestPath) .bodyValue(parts).retrieve().bodyToMono(String.class); System.out.println("mulit Return result:" + mono.block()); } public static void main(String[] args) { get(); post(); post2(); upload(); mulit(); } }