窥探-spring boot Deploy Web Application之Spring WebFlux
Spring WebFlux是Spring Framework 5.0中引入的新的响应式Web框架。与Spring MVC不同,它不需要Servlet API,是完全异步且无阻塞的,并通过Reactor项目实现Reactive Streams规范。
简单使用
spring-boot使用webflux需要使用spring-boot-starter-webflux的包,引入如下:
compile('org.springframework.boot:spring-boot-starter-webflux:2.3.1.RELEASE')
创建项目
创建一个gradle的子项目spring-boot-study-gradle-webflux,目录如下:
UserRouterConfig:配置相关的路由信息
@Configuration(proxyBeanMethods = false)
public class UserRouterConfig{
@Bean
public RouterFunction<ServerResponse> monoRouterFunction(UserHandler userHandler){
return RouterFunctions.route(RequestPredicates.GET("/users/list").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),userHandler::getUser)
.andRoute(RequestPredicates.GET("/users/{userId}").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),userHandler::getUserCustomers)
.andRoute(RequestPredicates.GET("/{userId}").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),userHandler::deleteUser);
}
}
UserHandler:用户操作相关方法
@Component
public class UserHandler {
private static List<Users> usersList = new ArrayList<>();
private static final Logger LOG = LoggerFactory.getLogger(UserHandler.class);
//初始化数据对象
static{
for(int i = 0 ;i < 10;i++){
Users users = new Users();
users.setId(i);
users.setUsername("username" + i);
users.setBrithday(new Date());
users.setAge(18 + i);
usersList.add(users);
}
}
public Mono<ServerResponse> getUser(ServerRequest request){
LOG.info("====查询用户信息=====");
//返回ServerResponse结果相应
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(usersList));
}
public Mono<ServerResponse> getUserCustomers(ServerRequest request){
LOG.info("======查询用户,根据编号:{}",request.pathVariable("userId"));
Integer id = Integer.valueOf(request.pathVariable("userId"));
Users user = null;
for(Users users : usersList){
if(users.getId() == id){
user = users;
}
}
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(user));
}
public Mono<ServerResponse> deleteUser(ServerRequest request){
return null;
}
}
Users:相关的实体类:
public class Users {
private Integer id;
private String username;
private Integer age;
private Date brithday;
public void setId(Integer id){
this.id = id;
}
public Integer getId(){
return this.id;
}
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return this.username;
}
public void setAge(Integer age){
this.age = age;
}
public Integer getAge(){
return this.age;
}
public void setBrithday(Date brithday){
this.brithday = brithday;
}
public Date getBrithday(){
return this.brithday;
}
}
Application:启动类
@SpringBootApplication
public class Application {
public static void main(String ... args){
SpringApplication.run(Application.class, args);
}
}
application.yml
server:
port: 8081
我们运行启动类
我们就可以直接访问相关的路由中设置的路径进行访问相关信息。
能访问返回数据说明成功了!
可以根据需要定义任意数量的RouterFunction bean,以对路由器的定义进行模块化。 如果需要应用优先级,可以对Bean进行排序。
在应用程序中添加spring-boot-starter-web和spring-boot-starter-webflux模块会导致Spring Boot自动配置Spring MVC,而不是WebFlux。之所以选择这种行为,是因为许多Spring开发人员将spring-boot-starter-webflux添加到其Spring MVC应用程序中以使用反应式WebClient。仍然可以通过将选定的应用程序类型设置为SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)来强制执行选择。
Spring WebFlux 自动配置
Spring Boot为Spring WebFlux提供了自动配置,可与大多数应用程序完美配合。
自动配置在Spring的默认设置之上添加了以下功能:
- 为HttpMessageReader和HttpMessageWriter实例配置编解码器
- 支持服务静态资源,包括对WebJars的支持
- 如果想保留Spring Boot WebFlux功能并想要添加其他WebFlux配置,则可以添加自定义类型为WebFluxConfigurer的@Configuration类,但不添加@EnableWebFlux。
- 如果要完全控制Spring WebFlux,则可以添加带有@EnableWebFlux注释的自己的@Configuration。