当前位置: 代码迷 >> 综合 >> Spring Boot 2.0 WebFlux
  详细解决方案

Spring Boot 2.0 WebFlux

热度:67   发布时间:2024-01-11 18:30:22.0

配置 POM 依赖

检查工程 POM 文件中,是否配置了 spring-boot-starter-webflux 依赖。如果是上面自动生成的,配置如下:

  <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

spring-boot-starter-webflux 依赖,是我们核心需要学习 webflux 的包,里面默认包含了 spring-boot-starter-reactor-netty 、spring 5 webflux 包。也就是说默认是通过 netty 启动的。

reactor-test、spring-boot-starter-test 两个依赖搭配是用于单元测试。

spring-boot-maven-plugin 是 Spring Boot Maven 插件,可以运行、编译等调用。

编写处理器类 Handler

新建包 org.spring.springboot.handler ,作为编写功能处理类。新建城市(City)例子的处理类 CityHandler,代码如下:

import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;@Component
public class CityHandler {public Mono<ServerResponse> helloCity(ServerRequest request) {return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromObject("Hello, City!"));}
}

ServerResponse 是对响应的封装,可以设置响应状态,响应头,响应正文。比如 ok 代表的是 200 响应码、MediaType 枚举是代表这文本内容类型、返回的是 String 的对象。

这里用 Mono 作为返回对象,是因为返回包含了一个 ServerResponse 对象,而不是多个元素。

编写路由器类 Router

新建 org.spring.springboot.router 包,作为编写路由器类。新建城市(City)例子的路由类 CityRouter,代码如下:

import org.spring.springboot.handler.CityHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;@Configuration
public class CityRouter {@Beanpublic RouterFunction<ServerResponse> routeCity(CityHandler cityHandler) {return RouterFunctions.route(RequestPredicates.GET("/hello").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),cityHandler::helloCity);}}

RouterFunctions 对请求路由处理类,即将请求路由到处理器。这里将一个 GET 请求 /hello 路由到处理器 cityHandler 的 helloCity 方法上。跟 Spring MVC 模式下的 HandleMapping 的作用类似。

RouterFunctions.route(RequestPredicate, HandlerFunction) 方法,对应的入参是请求参数和处理函数,如果请求匹配,就调用对应的处理器函数。

到这里一个简单的服务就写好了,下面怎么运行该服务。

启动运行项目

一个简单的 Spring Boot Webflux 工程就开发完毕了,下面运行工程验证下。使用 IDEA 右侧工具栏,点击 Maven Project Tab ,点击使用下 Maven 插件的 install 命令。或者使用命令行的形式,在工程根目录下,执行 Maven 清理和安装工程的指令:

cd springboot-webflux-1-quickstart
mvn clean install
  相关解决方案