ResultUtil
统一响应构建工具类,支持 MVC 和 WebFlux 两种编程模型
ResultUtil
ResultUtil 是统一响应构建工具类,用于构建符合 BaseResponse<T> 规范的响应。根据使用的框架不同,返回类型有所差异:
| 框架 | 包路径 | 返回类型 |
|---|---|---|
| Spring MVC | com.xlf.utility.mvc | ResponseEntity<BaseResponse<T>> |
| Spring WebFlux | com.xlf.utility.webflux | Mono<ResponseEntity<BaseResponse<T>>> |
方法签名
两种框架的方法签名完全相同,仅返回类型不同:
public static XxxResponse success(String message)
public static <T> XxxResponse success(String message, T data)
public static <T> XxxResponse error(ErrorCode errorCode, String errorMessage, T data)::: tip 返回类型说明
- MVC:
XxxResponse=ResponseEntity<BaseResponse<T>> - WebFlux:
XxxResponse=Mono<ResponseEntity<BaseResponse<T>>>:::
行为说明
字段
类型
响应示例
成功响应
{
"context": "550e8400-e29b-41d4-a716-446655440000",
"output": "Success",
"code": 200,
"message": "操作成功",
"duration": 15,
"data": null
}错误响应
{
"context": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
"output": "Unauthorized",
"code": 40101,
"message": "未授权",
"errorMessage": "令牌已过期",
"duration": 5
}使用示例
Spring MVC(同步)
@GetMapping("/{id}")
public ResponseEntity<BaseResponse<UserVO>> getUser(@PathVariable Long id) {
UserVO user = userService.getUserById(id);
return ResultUtil.success("查询成功", user);
}Spring WebFlux(响应式)
@GetMapping("/{id}")
public Mono<ResponseEntity<BaseResponse<UserDTO>>> getUser(@PathVariable Long id) {
return userService.getUserById(id)
.flatMap(user -> ResultUtil.success("获取成功", user))
.switchIfEmpty(ResultUtil.error(ErrorCode.RESOURCE_NOT_FOUND, "用户不存在", null));
}注意事项
success系列方法中的code使用200,不是ErrorCode中的业务码。error推荐在全局异常处理器中调用,业务层优先抛异常而不是直接返回错误响应。- 若请求未经过
ContextFilter,context与duration可能为null。