• (五)admin-boot项目之整合统一异常和统一返回值


    (五)整合统一异常和统一返回值

    项目地址:https://gitee.com/springzb/admin-boot
    如果觉得不错,给个 star

    简介:
    这是一个基础的企业级基础后端脚手架项目,主要由springboot为基础搭建,后期整合一些基础插件例如:redis、xxl-job、flowable、minioio、easyexcel、skyWalking、rabbitmq

    目录

    一、统一返回值

    统一返回结果 R

    package cn.mesmile.admin.common.result;
    
    import com.sun.istack.internal.Nullable;
    
    import java.io.Serializable;
    import java.util.Optional;
    
    /**
     * @author zb
     * @Description 公共返回信息
     */
    public class R<T> implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        /**
         * 状态码
         */
        private int code;
    
        /**
         * 是否成功
         */
        private boolean success;
    
        /**
         * 承载数据
         */
        private T data;
    
        /**
         * 返回消息
         */
        private String msg;
    
        private R(IResultCode resultCode) {
            this(resultCode, null, resultCode.getMessage());
        }
    
        private R(IResultCode resultCode, String msg) {
            this(resultCode, null, msg);
        }
    
        private R(IResultCode resultCode, T data) {
            this(resultCode, data, resultCode.getMessage());
        }
    
        private R(IResultCode resultCode, T data, String msg) {
            this(resultCode.getCode(), data, msg);
        }
    
        private R(int code, T data, String msg) {
            this.code = code;
            this.data = data;
            this.msg = msg;
            this.success = ResultCode.SUCCESS.code == code;
        }
    
        public static boolean isSuccess(@Nullable R<?> result) {
            return (Boolean) Optional.ofNullable(result).map((x) -> {
                return ResultCode.SUCCESS.code == x.code;
            }).orElse(Boolean.FALSE);
        }
    
        public static boolean isNotSuccess(@Nullable R<?> result) {
            return !isSuccess(result);
        }
    
        public static <T> R<T> data(T data) {
            return data(data, "操作成功");
        }
    
        public static <T> R<T> data(T data, String msg) {
            return data(200, data, msg);
        }
    
        public static <T> R<T> data(int code, T data, String msg) {
            return new R(code, data, data == null ? "暂无承载数据" : msg);
        }
    
        public static <T> R<T> success(String msg) {
            return new R(ResultCode.SUCCESS, msg);
        }
    
        public static <T> R<T> success(IResultCode resultCode) {
            return new R(resultCode);
        }
    
        public static <T> R<T> success(IResultCode resultCode, String msg) {
            return new R(resultCode, msg);
        }
    
        public static <T> R<T> fail(String msg) {
            return new R(ResultCode.FAILURE, msg);
        }
    
        public static <T> R<T> fail(int code, String msg) {
            return new R(code, (Object)null, msg);
        }
    
        public static <T> R<T> fail(IResultCode resultCode) {
            return new R(resultCode);
        }
    
        public static <T> R<T> fail(IResultCode resultCode, String msg) {
            return new R(resultCode, msg);
        }
    
        public static <T> R<T> status(boolean flag) {
            return flag ? success("操作成功") : fail("操作失败");
        }
    
        public int getCode() {
            return this.code;
        }
    
        public boolean isSuccess() {
            return this.success;
        }
    
        public T getData() {
            return this.data;
        }
    
        public String getMsg() {
            return this.msg;
        }
    
        public void setCode(final int code) {
            this.code = code;
        }
    
        public void setSuccess(final boolean success) {
            this.success = success;
        }
    
        public void setData(final T data) {
            this.data = data;
        }
    
        public void setMsg(final String msg) {
            this.msg = msg;
        }
    
        @Override
        public String toString() {
            return "R(code=" + this.getCode() + ", success=" + this.isSuccess() + ", data=" + this.getData() + ", msg=" + this.getMsg() + ")";
        }
    
        public R() {
        }
    }
    
    
    • 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
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153

    IResultCode

    package cn.mesmile.admin.common.result;
    
    import java.io.Serializable;
    
    /**
     * @author zb
     * @Description
     */
    public interface IResultCode extends Serializable {
        /**
         * 获取结果消息
         * @return 结果消息
         */
        String getMessage();
    
        /**
         *  获取返回状态码
         * @return 结果状态码
         */
        int getCode();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    ResultCode

    package cn.mesmile.admin.common.result;
    
    /**
     * @author zb
     * @Description
     */
    public enum ResultCode implements IResultCode {
        /**
         * 返回状态码,以及返回消息
         */
        SUCCESS(200, "操作成功"),
        FAILURE(400, "业务异常"),
        UN_AUTHORIZED(401, "请求未授权"),
        CLIENT_UN_AUTHORIZED(401, "客户端请求未授权"),
        NOT_FOUND(404, "404 没找到请求"),
        MSG_NOT_READABLE(400, "消息不能读取"),
        METHOD_NOT_SUPPORTED(405, "不支持当前请求方法"),
        MEDIA_TYPE_NOT_SUPPORTED(415, "不支持当前媒体类型"),
        REQ_REJECT(403, "请求被拒绝"),
        INTERNAL_SERVER_ERROR(500, "服务器异常"),
        PARAM_MISS(400, "缺少必要的请求参数"),
        PARAM_TYPE_ERROR(400, "请求参数类型错误"),
        PARAM_BIND_ERROR(400, "请求参数绑定错误"),
        PARAM_VALID_ERROR(400, "参数校验失败");
    
        final int code;
        final String message;
    
        @Override
        public int getCode() {
            return this.code;
        }
    
        @Override
        public String getMessage() {
            return this.message;
        }
    
        private ResultCode(final int code, final 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
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    测试

    package cn.mesmile.admin.modules.system.controller;
    
    import cn.mesmile.admin.common.result.R;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author zb
     * @Description
     */
    @RequestMapping("/api/v1/hello")
    @RestController
    public class HelloController {
    
        @GetMapping("/get")
        public R hello () {
            return R.data("hello world");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    启动 cn.mesmile.admin.AdminApplication 启动类,访问 localhost:8080/api/v1/hello/get

    看到统一返回结果如下:

    {
      "code": 200,
      "success": true,
      "data": "hello world",
      "msg": "操作成功"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    二、统一异常处理

    自定义异常

    package cn.mesmile.admin.common.exceptions;
    
    
    import cn.mesmile.admin.common.result.IResultCode;
    import cn.mesmile.admin.common.result.ResultCode;
    
    /**
     * @author zb
     * @Description 自定义业务异常
     */
    public class BusinessException extends RuntimeException {
    
        private final long serialVersionUID = 1L;
    
        private int code = ResultCode.FAILURE.getCode();
    
        private String msg = ResultCode.FAILURE.getMessage();
    
        public BusinessException() {
            super();
        }
    
        public BusinessException(String msg) {
            super(msg);
            this.msg = msg;
        }
    
        public BusinessException(IResultCode resultCode, String msg) {
            super(msg);
            this.code = resultCode.getCode();
            this.msg = msg;
        }
    
        public BusinessException(String msg, Throwable cause) {
            super(msg, cause);
            this.msg = msg;
        }
    
        public BusinessException(IResultCode resultCode, String msg, Throwable cause) {
            super(msg, cause);
            this.code = resultCode.getCode();
            this.msg = msg;
        }
    
        public BusinessException(Throwable cause) {
            super(cause);
        }
    
        public int getCode() {
            return code;
        }
    
        public void setCode(int code) {
            this.code = code;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    
    
    • 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
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    统一异常处理

    package cn.mesmile.admin.common.handler;
    
    import cn.mesmile.admin.common.constant.AdminConstant;
    import cn.mesmile.admin.common.exceptions.*;
    import cn.mesmile.admin.common.result.R;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.RestControllerAdvice;
    
    /**
     * @author zb
     * @Description 全局异常拦截
     * 

    * 如果我同时捕获了父类和子类,那么到底能够被那个异常处理器捕获呢?比如 Exception 和 BusinessException * 当然是 BusinessException 的异常处理器捕获了,精确匹配,如果没有 BusinessException 的异常处理器才会轮到它的 父亲 , * 父亲 没有才会到 祖父 。总之一句话, 精准匹配,找那个关系最近的 *

    */
    @Slf4j @RestControllerAdvice public class GlobalExceptionHandler { /** * @param businessException 业务异常 * @return @ResponseBody * @ExceptionHandler相当于controller的@RequestMapping 如果抛出的的是BusinessException,则调用该方法 */ @ExceptionHandler(BusinessException.class) public R handle(BusinessException businessException) { // 获取指定包名前缀的异常信息,减少不必要的日志 String stackTraceByPn = getStackTraceByPn(businessException, AdminConstant.basePackage); log.error("记录业务异常信息: 消息{} 编码{} {}", businessException.getMessage(), businessException.getCode(), stackTraceByPn); return R.fail(businessException.getCode(), businessException.getMessage()); } /** * 日志信息: 业务异常在全局处理的时候只记录了 指定头开始的日志,减少了堆栈信息 */ private String getStackTraceByPn(Throwable e, String packagePrefix) { StringBuilder append = new StringBuilder("\n").append(e); for (StackTraceElement stackTraceElement : e.getStackTrace()) { if (stackTraceElement.getClassName().startsWith(packagePrefix)) { append.append("\n\tat ").append(stackTraceElement); } } return append.toString(); } /** * 捕获空指针异常 **/ @ExceptionHandler(value = NullPointerException.class) public R handlerBindException(NullPointerException exception) { String message = exception.getMessage(); log.error("全局捕获null错误信息: {}", exception.toString(), exception); return R.fail(message); } /** * 捕获最大异常 **/ @ExceptionHandler(value = Exception.class) public R handlerBindException(Exception exception) { String message = exception.getMessage(); log.error("全局捕获错误信息: {}", exception.toString(), exception); return R.fail(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
    • 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
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    测试统一异常

    package cn.mesmile.admin.modules.system.controller;
    
    import cn.mesmile.admin.common.exceptions.BusinessException;
    import cn.mesmile.admin.common.result.R;
    import cn.mesmile.admin.common.result.ResultCode;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author zb
     * @Description
     */
    @Slf4j
    @RequestMapping("/api/v1/hello")
    @RestController
    public class HelloController {
    
        @GetMapping("/get")
        public R hello () {
            if (true){
                throw new BusinessException(ResultCode.FAILURE, "hello业务发送异常了");
            }
            return R.data("hello world");
        }
    
    }
    
    • 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

    启动 cn.mesmile.admin.AdminApplication 启动类,访问 localhost:8080/api/v1/hello/get

    看到统一返回结果如下:

    {
      "code": 400,
      "success": false,
      "data": null,
      "msg": "hello业务发送异常了"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    日志信息: 业务异常在全局处理的时候只记录了 指定头开始的日志,减少了堆栈信息

    2022-07-09 16:15:42.050  INFO 13512 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2022-07-09 16:15:42.051  INFO 13512 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2022-07-09 16:15:42.051  INFO 13512 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
    2022-07-09 16:15:42.074 ERROR 13512 --- [nio-8080-exec-1] c.m.a.c.handler.GlobalExceptionHandler   : 记录业务异常信息: 消息hello业务发送异常了 编码400 
    cn.mesmile.admin.common.exceptions.BusinessException: hello业务发送异常了
      at cn.mesmile.admin.modules.system.controller.HelloController.hello(HelloController.java:23)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    Apache Doris支持的数据类型详解
    Python之并发编程
    串行原理编程,中文编程工具中的串行构件,串行连接操作简单
    使用 spring-security-oauth2 体验 OAuth 2.0 的四种授权模式
    go高并发之路——go语言如何解决并发问题
    淘宝直播零代码弹窗生产方案总结
    Hibiki Run 市场火爆,“Listen to Earn”赛道的现象级应用?
    情侣天气推送升级简单版 & windows定时自动推送教程
    生产者-消费者问题详细分析【操作系统原理】
    腾讯云服务器2核4G、4核8G、8核16G、16核32G配置报价表出炉
  • 原文地址:https://blog.csdn.net/suprezheng/article/details/126322422