1. 介绍
Java中传统的RestTemplate 的主要问题在于不支持响应式流规范,也就无法提供非阻塞式的流式操作。而WebClient是响应式、非阻塞的客户端,属于Spring5中的spring-webflux库
2. 依赖
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
gradle依赖
dependencies {
compile 'org.springframework.boot:spring-boot-starter-webflux'
}
3. 使用
- WebClient实例创建
方式一:使用create方法创建
WebClient client = WebClient.create();
也可以创建的时候指定base URI
WebClient client = WebClient.create("http://localhost:8080");
方式二:最常用的方式是使DefaultWebClientBuilder类
WebClient client = WebClient.builder()
.baseUrl("http://localhost:8080")
.defaultCookie("cookieKey", "cookieValue&#