WebClient是一个响应式客户端,它提供了RestTemplate的替代方法。它公开了一个功能齐全、流畅的API,并依赖于非阻塞I / O,使其能够比RestTemplate更高效地支持高并发性。 WebClient 非常适合流式的传输方案,并且依赖于较低级别的HTTP客户端库来执行请求,是可插拔的。
如果在你系统的类路径上有Spring WebFlux,就可以选择使用WebClient来调用远程REST服务。相比之下RestTemplate,这个客户端具有更多的函数感并且完全Reactive响应式的。您可以 在Spring Framework文档 WebClient的专用 部分中 了解有关该内容的更多信息。
WebClient使用与WebFlux服务器应用程序相同的编解码器,并与服务器功能Web框架共享公共基本包,一些通用API和基础结构。API公开了Reactor Flux和Mono类型。默认情况下,它使用Reactor Netty作为HTTP客户端库,但其他人可以通过自定义ClientHttpConnector插入。
与RestTemplate相比,WebClient是:
RestTemplate不适合在非阻塞应用程序中使用,因此Spring WebFlux应用程序应始终使用WebClient。在大多数高并发场景中,WebClient也应该是Spring MVC中的首选,并且用于编写一系列远程,相互依赖的调用。
让我们创建一个新Spring Boot项目,包含org.springframework.boot:spring-boot-starter-webflux,org.projectlombok:lombok。使用Maven:
spring init --dependencies=webflux,lombok --language=maven --build=gradle spring-boot-webclient
pom.xml主要依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional><b>true</b></optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在这个例子中,我们将使用RESTFul在这个项目中实现一个服务案例,这是一个Android应用案例,主要用于改善你的饮料食谱健康方面的app,下面主要是服务器端方面,它将食谱和饮料暴露为API服务。
retrieve()方法
retrieve()方法是获取响应内容并对其进行解码的最简单方法:
@Service
<b>public</b> <b>class</b> BeverageServiceImpl implements BeverageService {
<b>private</b> WebClient client = WebClient.create(<font>"http://jugoterapia.josdem.io/jugoterapia-server"</font><font>);
<b>public</b> Mono<Beverage> getBeverage(Long id){
<b>return</b> client.get()
.uri(</font><font>"/beverages/{id}"</font><font>, id).accept(APPLICATION_JSON)
.retrieve()
.bodyToMono(Beverage.<b>class</b>);
}
}
</font>
饮料服务接口:
public interface BeverageService { Mono<Beverage> getBeverage(Long id); }
现在让我们创建一个简单的POJO来从我们的API中检索信息。
@Data
@AllArgsConstructor
@NoArgsConstructor
<b>public</b> <b>class</b> Beverage {
<b>private</b> Long id;
<b>private</b> String name;
<b>private</b> String ingredients;
<b>private</b> String recipe;
}
接下来,我们将CommandLineRunner用于启动我们的工作流程。这CommandLineRunner是Spring Boot中的回调接口,当Spring Boot启动时会调用它并通过run()内部方法传入args 。
@SpringBootApplication
<b>public</b> <b>class</b> DemoApplication {
<b>public</b> <b>static</b> <b>void</b> main(String[] args) {
SpringApplication.run(DemoApplication.<b>class</b>, args);
}
@Bean
CommandLineRunner run(BeverageService beverageService){
<b>return</b> args -> {
beverageService.getBeverage(35)
.subscribe(System.out::println);
};
}
}
以上源码下载: 此处