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是:
- 非阻塞,Reactive的,并支持更高的并发性和更少的硬件资源。
- 提供利用Java 8 lambdas的函数API。
- 支持同步和异步方案。
- 支持从服务器向上或向下流式传输。
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>true</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 public class BeverageServiceImpl implements BeverageService {
private WebClient client = WebClient.create("http://jugoterapia.josdem.io/jugoterapia-server");
public Mono<Beverage> getBeverage(Long id){ return client.get() .uri("/beverages/{id}", id).accept(APPLICATION_JSON) .retrieve() .bodyToMono(Beverage.class); }
}
|
饮料服务接口:
public interface BeverageService { Mono getBeverage(Long id); }
现在让我们创建一个简单的POJO来从我们的API中检索信息。
@Data @AllArgsConstructor @NoArgsConstructor public class Beverage {
private Long id; private String name; private String ingredients; private String recipe;
}
|
接下来,我们将CommandLineRunner用于启动我们的工作流程。这CommandLineRunner是Spring Boot中的回调接口,当Spring Boot启动时会调用它并通过run()内部方法传入args 。
@SpringBootApplication public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }
@Bean CommandLineRunner run(BeverageService beverageService){ return args -> { beverageService.getBeverage(35) .subscribe(System.out::println); }; }
}
|
以上源码下载:此处