目录
官网:https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html
1.WebFlux介绍
Spring WebFlux 是 Spring Framework 5.0中引入的新的响应式web框架。与Spring MVC不同,它不需要Servlet API,是完全异步且非阻塞的,并且通过Reactor项目实现了Reactive Streams规范。
- Mono:实现发布者,即要么成功发布并返回 0 或 1 个元素,要么错误。
- Flux:实现发布者,即要么成功发布 0 到 N 个元素,要么错误。
2.bean方式路由
2.0.引入pom
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2.1.编写处理器类 Handler
@Slf4j
@Component
public class UserHandler {
private User user;
@PostConstruct
public void init() {
User user = new User()
.setPhone("12345678901")
.setUserId(1L)
.setUserName("WebFlux");
this.user = user;
}
public Mono<ServerResponse> helloUser(ServerRequest request) {
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(user));
}
}
2.2.编写路由器类 Router
@Configuration
public class UserRouter {
@Bean
public RouterFunction<ServerResponse> routeUser(UserHandler userHandler) {
return RouterFunctions.route(RequestPredicates.GET("/bean/webflux")
.and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::helloUser);
}
}
2.3.访问测试
3.使用自动配置加注解驱动的模式
3.1.编写处理器类 Handler
@Slf4j
@Component
public class UserHandler {
private User user;
@PostConstruct
public void init() {
User user = new User()
.setPhone("12345678901")
.setUserId(1L)
.setUserName("WebFlux");
this.user = user;
}
public Flux<User> listUser() {
List<User> userList = new ArrayList<>();
userList.add(user);
return Flux.fromIterable(userList);
}
public Mono<User> getById(Long id) {
log.debug("查询用户:{}", id);
return Mono.justOrEmpty(user);
}
public Mono<Boolean> saveUser(User user) {
log.debug("新增用户:{}", user);
return Mono.create(userMonoSink -> userMonoSink.success());
}
public Mono<Boolean> updateUser(User user) {
log.debug("修改用户:{}", user);
return Mono.create(userMonoSink -> userMonoSink.success());
}
public Mono<Boolean> removeUser(Long userId) {
log.debug("删除用户:{}", userId);
return Mono.create(userMonoSink -> userMonoSink.success());
}
}
3.2.编写controller
@AllArgsConstructor
@RestController
@RequestMapping("/webflux")
public class WebFluxController {
private final UserHandler userHandler;
@GetMapping("/list")
public Flux<User> listUser() {
return userHandler.listUser();
}
@GetMapping("/{id}")
public Mono<User> getById(@PathVariable("id") Long id) {
return userHandler.getById(id);
}
@PostMapping
public Mono<Boolean> saveUser(@RequestBody User user) {
return userHandler.saveUser(user);
}
@PutMapping
public Mono<Boolean> updateUser(@RequestBody User user) {
return userHandler.updateUser(user);
}
@DeleteMapping("/{id}")
public Mono<Boolean> deleteUser(@PathVariable("id") Long id) {
return userHandler.removeUser(id);
}
}
3.3.postman测试
2022-01-16 10:48:19.987 DEBUG 3540 --- [ctor-http-nio-3] top.dongxibao.demo.handler.UserHandler : 查询用户:1
2022-01-16 10:48:43.028 DEBUG 3540 --- [ctor-http-nio-3] top.dongxibao.demo.handler.UserHandler : 新增用户:User(userId=null, userName=null, phone=null)
2022-01-16 10:49:04.686 DEBUG 3540 --- [ctor-http-nio-3] top.dongxibao.demo.handler.UserHandler : 修改用户:User(userId=null, userName=null, phone=null)
2022-01-16 10:49:36.560 DEBUG 3540 --- [ctor-http-nio-3] top.dongxibao.demo.handler.UserHandler : 删除用户:1