目录
2、准备一个类,实现ResponseBodyAdvice接口
关于SpringBoot的同一功能处理,本博主在这篇博客已经有介绍过:http://t.csdnimg.cn/pTyFL
不去看也没关系,本篇博客手把手教会你 ~
首先,我们准备一个类,这个类就作为所有的返回值,这个类中,设置三个字段:状态码 + 数据 + 错误信息
代码示例:
- package com.example.demo.common;
-
- import lombok.Data;
-
- /**
- * Created with IntelliJ IDEA.
- * Description:
- * User:龙宝
- * Date:2023-10-08
- * Time:14:50
- * 统一返回
- */
- @Data
- public class AjaxResult {
- private Integer code;//状态码
- private String msg;//状态码描述
- private Object data;//返回数据
-
- /*
- 操作成功返回结果
- */
- public static AjaxResult success(Object data) {
- AjaxResult ajaxResult = new AjaxResult();
- ajaxResult.setCode(200);
- ajaxResult.setMsg("");
- ajaxResult.setData(data);
- return ajaxResult;
- }
- public static AjaxResult success(Integer code,Object data) {
- AjaxResult ajaxResult = new AjaxResult();
- ajaxResult.setCode(code);
- ajaxResult.setMsg("");
- ajaxResult.setData(data);
- return ajaxResult;
- }
-
- public static AjaxResult success(Integer code,String msg,Object data) {
- AjaxResult ajaxResult = new AjaxResult();
- ajaxResult.setCode(code);
- ajaxResult.setMsg(msg);
- ajaxResult.setData(data);
- return ajaxResult;
- }
-
- /*
- 错误返回结果
- */
- public static AjaxResult fail(Integer code,String msg) {
- AjaxResult ajaxResult = new AjaxResult();
- ajaxResult.setCode(code);
- ajaxResult.setMsg(msg);
- ajaxResult.setData("");
- return ajaxResult;
- }
- public static AjaxResult fail(String msg) {
- AjaxResult ajaxResult = new AjaxResult();
- ajaxResult.setCode(-1);
- ajaxResult.setMsg(msg);
- ajaxResult.setData("");
- return ajaxResult;
- }
-
- }
上述代码中,我们可以看到,我们还提供了几个静态方法,分为两大类:success 和fail 即后端能够正确处理业务的返回,和后端判断参数有误等其他的业务返回。例如,用户登录:用户密码输入错误为有误返回 ; 正常登录上系统 为 正确返回~
这样一来,前端只需要接收到一个AjaxResult这样的对象,检查他的状态码code的值,就可以这个返回的是否是正确的值,正确的按AjaxResult的数据data来处理,错误的按msg的值给出对应提示~
准备一个类,并添加@ControllerAdvice注解
例:
- @ControllerAdvice
- public class ResponseAdvice{
-
- }
实现ResponseBodyAdvice接口,重写supports和beforeBodyWrite(统一对象就是此方法中实现)
例:
- @ControllerAdvice
- public class ResponseAdvice implements ResponseBodyAdvice {
-
- @Override
- public boolean supports(MethodParameter returnType, Class converterType) {
- return true;
- }
- @SneakyThrows
- @Override
- public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
- return body;
- }
- }
说明:
- @ControllerAdvice
- public class ResponseAdvice implements ResponseBodyAdvice {
-
- @Override
- public boolean supports(MethodParameter returnType, Class converterType) {
- return true;
- }
- @SneakyThrows
- @Override
- public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
- return AjaxResult.success(body);
- }
- }
这样的写法其实并不好,因为我们好像就只是默认返回的值都是AjaxResult的success方法,不能灵活调整~ 使用下面的方式会更好
例如上述举例中,登陆成功后,返回给前端四个字 return "登陆成功~";
我们大可以写成这样:

这种实现,我们不管是返回正确的值还是非正确的值,都是可以的,无非就是调用success还是fail方法~
然后在上述实现ResponseBodyAdvice的接口中的第二个方法,返回值就直接设置为 :

如果说,在上述实现ResponseBodyAdvice的接口中的第二个方法中,我们直接返回body,会出现两个有误的情况:
情况一:如果开发人员的返回值,忘记调用AjaxResult的方法了,前端解析数据时,会出现异常
因此,我们要有一个判断,代码,例:
- @SneakyThrows
- @Override
- public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
- if(body instanceof AjaxResult) {
- return body;
- }
- return AjaxResult.success(body);
- }
情况二:如果body是字符串,会出现报错
为什么呢?String是一个很特殊的类型,既不是基本类型,也不属于对象,并且在重写方法时,除String以外都是使用同一个格式化工具,而String用的是自己的一套格式化工具,因此在转换成HashMap时,还没有被加载好,而其他的转换器都已经加载好了,就会出现异常了。
因此当返回类型为String类型时,就要进行特殊处理,使用JSON的writeValueAsString方法将java对象转换成JSON格式再返回~
代码修改如下:

代码:
- package com.example.demo.config;
-
- import com.example.demo.common.AjaxResult;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import lombok.SneakyThrows;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.MethodParameter;
- import org.springframework.http.MediaType;
- import org.springframework.http.server.ServerHttpRequest;
- import org.springframework.http.server.ServerHttpResponse;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
-
- /**
- * Created with IntelliJ IDEA.
- * Description:
- * User:龙宝
- * Date:2023-10-08
- * Time:15:11
- */
- @ControllerAdvice
- public class ResponseAdvice implements ResponseBodyAdvice {
- @Autowired
- private ObjectMapper objectMapper;
-
- @Override
- public boolean supports(MethodParameter returnType, Class converterType) {
- return true;
- }
- @SneakyThrows
- @Override
- public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
- if(body instanceof AjaxResult) {
- return body;
- }
- if(body instanceof String) {
- return objectMapper.writeValueAsString(body);
- }
- return AjaxResult.success(body);
- }
- }
总的来说就是两个步骤:创建一个AjaxResult类,然后来一个实现ResponseBodyAdvice接口的类,就可以了~
好啦,上述就是我在OJ项目中对返回值的统一处理。希望对你有所帮助~