@ControllerAdvice实现全局响应结果格式化处理
创始人
2025-05-31 13:48:10
0

定义ResultCode

这里先简单定义四个状态:101,102,103,200,如下

@AllArgsConstructor
@NoArgsConstructor
@Getter
public enum ResultCode {FAILURE(100, "请求失败"),TOKEN_EXPIRED(101, "请登录"),NO_PERMISSION(102, "无权限"),SUCCESS(200, "请求成功");private Integer code;private String description;
}

构造响应数据结构

数据结构分为两种,一种带分页,一种不带分页。

/**
*
* @author  yangyile
* @since 2023-03-18
*/
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class ResultInfo implements Serializable {private Object data;private Integer code;private String msg;private Integer total;public ResultInfo(Object data, Integer code, String msg) {this.data = data;this.code = code;this.msg = msg;}public static ResultInfo success(Object data) {return new ResultInfo(data, ResultCode.SUCCESS.getCode(), "success");}public static ResultInfo success(Object data, Integer total) {return new ResultInfo(data, ResultCode.SUCCESS.getCode(), "success", total);}public static ResultInfo success(Object data, Integer code, String msg) {return new ResultInfo(data, code, msg);}public static ResultInfo success(Object data, Integer code, String msg, Integer total) {return new ResultInfo(data, code, msg, total);}public static ResultInfo fail(Object data, Integer code, String msg) {return new ResultInfo(data, code, msg);}
}

定义响应格式化注解

该注解放在类上。告诉spring容器,该类下所有http请求方法返回的数据都会被包装。

@ControllerAdvice(annotations = {ResponseFormat.class})
public class GlobalResponseInterceptor implements ResponseBodyAdvice {@Overridepublic boolean supports(MethodParameter methodParameter, Class> aClass) {return true;}@Overridepublic Object beforeBodyWrite(Object body,MethodParameter methodParameter,MediaType mediaType,Class> aClass,ServerHttpRequest serverHttpRequest,ServerHttpResponse serverHttpResponse) {final String returnTypeName = methodParameter.getParameterType().getName();if("void".equals(returnTypeName)) {// 返回类型为voidreturn ResultInfo.success(null);} else if(!mediaType.includes(MediaType.APPLICATION_JSON) || "com.yyl.system.response.ResultInfo".equals(returnTypeName)) {// 返回类型为json字符串或者为ResultInforeturn body;} else if(body instanceof ArrayList) {// 返回结果为List,但没有分页return ResultInfo.success(body, ((ArrayList) body).size());} else if(body instanceof HashMap && (Boolean) ((HashMap) body).get("total")) {// 返回结果带有分页return ResultInfo.success(body, (Integer) ((HashMap) body).get("total"));}return ResultInfo.success(body);}
}
 

@ControllerAdvice注解

@ControllerAdvice注解是Spring3.2中新增的注解,学名是Controller增强器,作用是给Controller控制器添加统一的操作或处理。
@ControllerAdvice(annotations = {ResponseFormat.class}),改行代码的意思就是以@ResponseFormat注解为切入点,@ResponseFormat注解的类的所有controller方法都要经过处理后返回。

重写beforeBodyWrite方法

参数说明:

  • body:controller方法返回的数据;
  • methodParameter:controller方法的有关信息,参数、返回类型等等;
  • aClass:报文信息转换器,可将java对象转化为JSON格式的响应报文,在这里可以对响应体进行改写;
  • serverHttpRequest:请求体;
  • serverHttpResponse:响应体;
    该方法体中有5个分支来处理响应结果返回:
  • 返回类型为void,data直接返回null;
  • 如果返回格式化的json数据或者ResultInfo格式数据,那么直接返回body;
  • 如果返回ArrayList,但没有分页,那么返回body和数据总数;
  • 如果返回ArrayList,并带有分页,则返回分页数据及数据总数;
  • 其它情况,则返回body;

使用示例

@Api(tags = {"系统用户"})
@RestController
@RequestMapping("/user")
@ResponseFormat
public class UserController {@Autowiredprivate IuserService userService;@ApiOperation(value = "获取用户列表", notes = "获取用户列表,支持分页和根据userName关键字查询")@ApiImplicitParams({@ApiImplicitParam(name = "page", value = "", paramType = "query", dataType = "Integer"),@ApiImplicitParam(name = "limit", value = "", paramType = "query", dataType = "String"),@ApiImplicitParam(name = "userName", value = "用户名", paramType = "query", dataType = "String")})@GetMapping("/getUserList")public List> getUserList() {return userService.getUserList();}@ApiOperation(value = "添加用户", notes = "添加用户")@ApiImplicitParams({@ApiImplicitParam(name = "userName", required = true, value = "用户名", paramType = "query", dataType = "String"),@ApiImplicitParam(name = "password", required = true, value = "密码", paramType = "query", dataType = "String"),@ApiImplicitParam(name = "isEnable", required = true, value = "是否启用", paramType = "query", dataType = "Integer"),@ApiImplicitParam(name = "nickName", value = "别名", paramType = "query", dataType = "String"),@ApiImplicitParam(name = "avatar", value = "头像", paramType = "query", dataType = "String"),@ApiImplicitParam(name = "age", value = "年龄", paramType = "query", dataType = "Integer"),@ApiImplicitParam(name = "birthday", value = "生日", paramType = "query", dataType = "Date"),@ApiImplicitParam(name = "hobby", value = "爱好", paramType = "query", dataType = "String")})@GetMapping("/addUser")public Integer addUser(UserEntity user) {return userService.addUser(user);}
}

给UserController类添加注解@ResponseFormat,表示其方法返回数据会被全局处理,然后返回。

效果

{"data": [{"id": 2,"userName": "test","password": "123","isEnable": 1,"nickName": null,"avatar": null,"age": null,"birthday": null,"hobby": null,"updateTime": "2023-03-18T12:27:26.000+0000","createTime": "2023-03-18T12:27:26.000+0000"},{"id": 3,"userName": "test","password": "123","isEnable": 1,"nickName": null,"avatar": null,"age": null,"birthday": null,"hobby": null,"updateTime": "2023-03-18T12:35:26.000+0000","createTime": "2023-03-18T12:35:26.000+0000"}],"code": 200,"msg": "success","total": 2
}

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...