WebClient是Spring提供的非阻塞、响应式的Http客户端,提供同步及异步的API,将会代替RestTemplate及AsyncRestTemplate。文中所使用到的软件版本:Java 1.8.0_191、SpringBoot 2.2.1.RELEASE。
使用WebClient需要用到Reactor Netty,依赖如下:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webflux</artifactId> </dependency> <dependency> <groupId>io.projectreactor.netty</groupId> <artifactId>reactor-netty</artifactId> </dependency>
public static void get() { String requestPath = "http://localhost:8080/demo/httptest/getUser?userId=1000&userName=李白"; WebClient webClient = WebClient.create(); Mono<String> mono = webClient.get().uri(requestPath).retrieve().bodyToMono(String.class); //同步方式 System.out.println("get block返回结果:" + mono.block()); //异步方式 final CountDownLatch latch = new CountDownLatch(5); for (int i = 0; i < 5; i++) { requestPath = "http://localhost:8080/demo/httptest/getUser?userId=1000&userName=李白" + 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返回结果:" + s); } }); } try { latch.await(); } catch (Exception e) { e.printStackTrace(); } }
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", "李白"); Mono<String> mono = webClient.post().uri(requestPath).bodyValue(map).retrieve().bodyToMono(String.class); System.out.println("post返回结果:" + mono.block()); }
public static void post2() { String requestPath = "http://localhost:8080/demo/httptest/addUser"; WebClient webClient = WebClient.create(); String param = "{\"userId\": \"1001\",\"userName\":\"杜甫\"}"; Mono<String> mono = webClient.post().uri(requestPath).contentType(MediaType.APPLICATION_JSON).bodyValue(param) .retrieve().bodyToMono(String.class); System.out.println("post json返回结果:" + mono.block()); }
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返回结果:" + mono.block()); }
public static void mulit() { String requestPath = "http://localhost:8080/demo/httptest/multi"; WebClient webClient = WebClient.create(); MultipartBodyBuilder builder = new MultipartBodyBuilder(); builder.part("param1", "参数1"); builder.part("param2", "参数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返回结果:" + mono.block()); }
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; /** * * 通过WebClient调用Http接口 * */ public class WebClientCase { /** * GET请求 */ public static void get() { String requestPath = "http://localhost:8080/demo/httptest/getUser?userId=1000&userName=李白"; WebClient webClient = WebClient.create(); Mono<String> mono = webClient.get().uri(requestPath).retrieve().bodyToMono(String.class); //同步方式 System.out.println("get block返回结果:" + mono.block()); //异步方式 final CountDownLatch latch = new CountDownLatch(5); for (int i = 0; i < 5; i++) { requestPath = "http://localhost:8080/demo/httptest/getUser?userId=1000&userName=李白" + 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返回结果:" + s); } }); } try { latch.await(); } catch (Exception e) { e.printStackTrace(); } } /** * POST请求(发送键值对数据) */ 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", "李白"); Mono<String> mono = webClient.post().uri(requestPath).bodyValue(map).retrieve().bodyToMono(String.class); System.out.println("post返回结果:" + mono.block()); } /** * POST请求(发送json数据) */ public static void post2() { String requestPath = "http://localhost:8080/demo/httptest/addUser"; WebClient webClient = WebClient.create(); String param = "{\"userId\": \"1001\",\"userName\":\"杜甫\"}"; Mono<String> mono = webClient.post().uri(requestPath).contentType(MediaType.APPLICATION_JSON).bodyValue(param) .retrieve().bodyToMono(String.class); System.out.println("post json返回结果:" + mono.block()); } /** * 上传文件 */ 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返回结果:" + mono.block()); } /** * 上传文件及发送键值对数据 */ public static void mulit() { String requestPath = "http://localhost:8080/demo/httptest/multi"; WebClient webClient = WebClient.create(); MultipartBodyBuilder builder = new MultipartBodyBuilder(); builder.part("param1", "参数1"); builder.part("param2", "参数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返回结果:" + mono.block()); } public static void main(String[] args) { get(); post(); post2(); upload(); mulit(); } }
Java调用Http接口(7,end)--WebClient调用Http接口
原文:https://www.cnblogs.com/wuyongyin/p/11959240.html