在 Spring Boot 3.4 中,Spring 提供了一种新的函数式风格的 API,特别是在使用 Spring Web 和 Spring Data 时,可以用更加简洁和函数式的方式来构建应用程序。这种函数式编程方式主要通过 RouterFunction 和 HandlerFunction 来替代传统的基于注解的方式。这样做可以使代码更加灵活且模块化。
下面是将你提供的代码转换为 Spring Boot 3.4 函数式风格 API 的例子:
1. 传统方式 (基于注解的方式)
/**
- 系统基础信息–商品订单信息表管理模块
- @author weimeilayer@gmail.com ✨
- @date 💓💕 2024-12-26 15:02:31 🐬🐇 💓💕
*/
@RestController
public class OrderController {
@SysLog("分页查询系统基础信息--商品订单信息表")
@Operation(summary = "分页查询系统基础信息--商品订单信息表", description = "分页查询系统基础信息--商品订单信息表")
@GetMapping("/pagelist")
public Result<IPage<PayGoodsOrderDto>> getPayGoodsOrderPage(@ParameterObject Page page, @ParameterObject PayGoodsOrderSelVo selvo) {
return Result.ok(payGoodsOrderService.getPayGoodsOrderDtoPage(page, selvo));
}
}
2. 函数式风格 (Spring Boot 3.4)
首先,你需要确保启用了 Spring WebFlux 来支持函数式 API。然后,你可以使用 RouterFunction 和 HandlerFunction 来定义路由和处理函数。
/**
* 系统基础信息--商品订单信息表管理模块
* @author weimeilayer@gmail.com ✨
* @date 💓💕 2024-12-26 15:02:31 🐬🐇 💓💕
*/
@Configuration
public class OrderRouterConfig {
@Bean
public RouterFunction<ServerResponse> route(OrderHandler orderHandler) {
return RouterFunctions
.route(RequestPredicates.GET("/pagelist")
.and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
orderHandler::getPayGoodsOrderPage);
}
}
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.*;
/**
* 系统基础信息--商品订单信息表管理模块
* @author weimeilayer@gmail.com ✨
* @date 💓💕 2024-12-26 15:02:31 🐬🐇 💓💕
*/
@Component
public class OrderHandler {
private final PayGoodsOrderService payGoodsOrderService;
public OrderHandler(PayGoodsOrderService payGoodsOrderService) {
this.payGoodsOrderService = payGoodsOrderService;
}
@Operation(summary = "分页查询系统基础信息--商品订单信息表", description = "分页查询系统基础信息--商品订单信息表")
public Mono<ServerResponse> getPayGoodsOrderPage(ServerRequest request) {
Page page = request.queryParam("page").map(Page::new).orElse(Page.DEFAULT);
PayGoodsOrderSelVo selvo = request.queryParam("selvo").map(PayGoodsOrderSelVo::new).orElse(new PayGoodsOrderSelVo());
IPage<PayGoodsOrderDto> result = payGoodsOrderService.getPayGoodsOrderDtoPage(page, selvo);
return ServerResponse.ok().bodyValue(Result.ok(result));
}
}
3. 解释
RouterFunction 和 ServerResponse:
RouterFunction 是用于定义请求路由的函数,接受 ServerRequest 对象并返回 ServerResponse 对象。
ServerResponse 用于构建响应,可以通过 ServerResponse.ok().bodyValue(…) 返回数据。
RequestPredicates:
RequestPredicates.GET(“/pagelist”) 表示匹配 HTTP GET 请求,并将其映射到 /pagelist 路径。
RequestPredicates.accept(MediaType.APPLICATION_JSON) 表示只接受 JSON 格式的请求。
HandlerFunction:
处理请求的函数,可以将分页查询逻辑提取到 OrderHandler 类中,避免了在控制器类中直接进行复杂的业务逻辑编写。
Mono 和 ServerRequest:
使用 Mono 来表示响应体,这符合 WebFlux 异步非阻塞的特性。
ServerRequest 提供了对请求的访问,允许从 URL、查询参数等地方提取数据。
4. 如何使用
如果你想在 Spring Boot 项目中启用函数式编程风格的 API,你可以做以下配置:
确保 WebFlux 可用:
在 pom.xml 中包含 spring-boot-starter-webflux 依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
启用函数式编程:
将你的路由定义在 @Configuration 类中,或者使用 Java 配置的方式来启用。
5. 优点
灵活性:可以更灵活地定义路由和处理函数,适合微服务架构。
模块化:可以将处理逻辑和路由分开,使代码更加清晰。