• @ControllerAdvice + ResponseBodyAdvice实现返回统一结构


    实现前的准备工作

    @ControllerAdvice注解的简单介绍

    @ControllerAdvice注解作用是给Controller控制器添加统一的操作或处理。
    对于@ControllerAdvice,我们比较熟知的用法是结合@ExceptionHandler用于全局异常的处理(之前总结过一篇全局异常捕获的文章【参数校验 + 全局异常拦截】),但其作用不止于此。ControllerAdvice拆开来就是Controller Advice,关于Advice,在Spring的AOP中,是用来封装一个切面所有属性的,包括切入点和需要织入的切面逻辑。这里ControllerAdvice也可以这么理解,其抽象级别应该是用于对Controller进行切面环绕的,而具体的业务织入方式则是通过结合其他的注解来实现的。

    ResponseBodyAdvice接口的简单介绍

    使用场景

    ResponseBodyAdvice可以在注解@ResponseBody将返回值处理成相应格式之前操作返回值。实现这个接口即可完成相应操作。可用于对response 数据的一些统一封装或者加密等操作

    作用范围

    ResponseBodyAdvice是SpringMVC4.1提供的一个接口,它允许在 执行 @ResponseBody后自定义返回数据,或者将返回@ResponseEntity的 Controller Method在写入主体前使用HttpMessageConverter进行自定义操作。由此可见,它的作用范围为:

    • 使用@ResponseBody注解进行标记
    • 返回@ResponseEntity

    接口方法分析

    源码:

    package org.springframework.web.servlet.mvc.method.annotation;
    
    import org.springframework.core.MethodParameter;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.server.ServerHttpRequest;
    import org.springframework.http.server.ServerHttpResponse;
    import org.springframework.lang.Nullable;
    
    public interface ResponseBodyAdvice<T> {
        boolean supports(MethodParameter var1, Class<? extends HttpMessageConverter<?>> var2);
    
        @Nullable
        T beforeBodyWrite(@Nullable T var1, MethodParameter var2, MediaType var3, Class<? extends HttpMessageConverter<?>> var4, ServerHttpRequest var5, ServerHttpResponse var6);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • supports:该方法返回true时,才会进去下面的 beforeBodyWrite方法。该方法可以添加一些判断条件
    • beforeBodyWrite:body写入前的操作

    自定义辅助类

    随便写个实体类

    import lombok.Data;
    
    import java.util.List;
    @Data
    public class UserVo {
    
        private String name;
    
        private Integer age;
    
        private String addr;
    
        private List<String> language;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    为了返回统一的数据,我们还可以自定义一个返回类ResultVO

    import lombok.Data;
    
    import java.io.Serializable;
    
    /**
     * @author coderzpw.zhang
     * @version 1.0
     */
    @Data
    public class ResultVO<T> implements Serializable {
    
        // 状态码
        private Integer code;
        // 提示信息
        private String msg;
        // 具体内容
        private T data;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    自定义通用枚举类

    import lombok.Getter;
    
    /**
     * @author coderzpw.zhang
     * @version 1.0
     */
    @Getter
    public enum ResultEnum {
    
        SUCCESS(200, "成功!"),
        UN_EXPECTED(500, "系统发生错误,请联系管理员!"),
        UN_AUTHORIZED(401, "未认证!"),
        NO_PERMISSION(403, "无权限!");
    
    
    
        private Integer code;
        private String message;
    
        ResultEnum(Integer code, String message) {
            this.code = code;
            this.message = message;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    然后再写一个生成ResultVo对象的工具类

    import com.coderzpw.exception.vo.ResultVO;
    import com.coderzpw.exception.enums.ResultEnum;
    
    /**
     * @author coderzpw.zhang
     * @version 1.0
     */
    public class ResultVOUtil {
        /**
         * 返回成功信息(带返回数据)
         * @param object
         * @return
         */
        public static ResultVO success(Object object) {
            ResultVO resultVO = new ResultVO();
            resultVO.setData(object);
            resultVO.setCode(ResultEnum.SUCCESS.getCode());
            resultVO.setMsg(ResultEnum.SUCCESS.getMessage());
            return resultVO;
        }
    
        /**
         * 返回成功信息(不带数据)
         * @return
         */
        public static ResultVO success() {
            return success(null);
        }
    
        /**
         * 返回错误数据
         * @param code
         * @param msg
         * @return
         */
        public static ResultVO error(Integer code, String msg) {
            ResultVO resultVO = new ResultVO();
            resultVO.setCode(code);
            resultVO.setMsg(msg);
            return resultVO;
        }
    
        /**
         * 返回错误数据(枚举类型入参)
         * @param resultEnum
         * @return
         */
        public static ResultVO error(ResultEnum resultEnum) {
            ResultVO resultVO = new ResultVO();
            resultVO.setCode(resultEnum.getCode());
            resultVO.setMsg(resultEnum.getMessage());
            return resultVO;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    开始实现

    添加一个响应拦截类

    import com.alibaba.fastjson.JSONObject;
    import com.coderzpw.exception.utils.ResultVOUtil;
    import com.coderzpw.exception.vo.ResultVO;
    import org.springframework.core.MethodParameter;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.server.ServerHttpRequest;
    import org.springframework.http.server.ServerHttpResponse;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
    
    /**
     * @author coderzpw.zhang
     * @version 1.0
     */
    @ControllerAdvice
    public class CommonResultResponseAdvice implements ResponseBodyAdvice<Object> {
    
    
        @Override
        public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
            // 直接返回 true,则会处理所有符合条件的返回。如有特殊要求条件,可以在该方法中自行加入拦截逻辑
            return true;
        }
    
        @Override
        public Object beforeBodyWrite(Object o,
                                      MethodParameter methodParameter,
                                      MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass,
                                      ServerHttpRequest serverHttpRequest,
                                      ServerHttpResponse serverHttpResponse) {
            // 如果直接响应字符串返回,则会报类型转换异常.
            if (o instanceof String) {
                return JSONObject.toJSONString(ResultVOUtil.success(o));
            }
            // 如果已经是ResultVO类型, 就没必要再包装一层了
            if (o instanceof ResultVO) {
                return o;
            }
    
            return ResultVOUtil.success(o);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    添加一个web接口类

    import com.coderzpw.exception.vo.UserVo;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author coderzpw.zhang
     * @version 1.0
     */
    @RestController
    public class TestController {
    
        @GetMapping("/test1")
        public Object test1() {
            UserVo userVo = new UserVo();
            userVo.setName("coderzpw.zhang");
            userVo.setAge(22);
            userVo.setAddr("四川省.成都市.高新区");
            return userVo;
        }
    
        @GetMapping("/test2")
        public String test2() {
            return "hello";
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    测试响应结果

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    vue3 setup 语法糖,父子传参
    Python 自定义模块和包设计英语生词本(文件版)
    python 采用selenium+cookies 获取登录后的网页
    使用frida来spawn Fork 的子进程
    通用/参考电路/ 连接备份
    量子笔记:量子计算 toy python implementation from scratch
    【SpringSecurity】三更草堂项目案例分析2 - 认证主体业务编写
    正则表达式.java
    【Kafka系列 06】Kafka Producer源码解析
    springCloudAlibaba之服务熔断组件---sentinel
  • 原文地址:https://blog.csdn.net/qq_45464560/article/details/126325383