• SpringBoot 统一异常处理


    1. 统一返回结果集

    package com.liming.pojo;
    
    import com.liming.exception.AppExceptionCodeMsg;
    import lombok.Data;
    
    /**
     * 全局统一返回结果类
     * @author 黎明
     * @date 2023/9/6 14:11
     * @version 1.0
     */
    @Data
    public class Result<T> {
    
        private Integer code; // 状态码
        private String msg; //返回消息
        private T data; //返回数据
    
        /**
         * 私有化构造方法,禁止在其它类创建对象
         */
        private Result(Integer code,String msg,T data){
            this.code = code;
            this.msg = msg;
            this.data = data;
        }
    
        // 不带自定义消息响应成功的方法
        public static <T> Result success(T data){
            Result resp = new Result(200, "success", data);
            return resp;
        }
    
        // 带自定义消息响应成功的方法
        public static <T> Result success(String msg, T data){
            Result resp = new Result(200,msg, data);
            return resp;
        }
    
        // 自定义异常返回的结果
        public static <T> Result error(AppExceptionCodeMsg appExceptionCodeMsg){
            Result resp = new Result(appExceptionCodeMsg.getCode(), appExceptionCodeMsg.getMsg(), null);
            return resp;
        }
    
        // 其他异常返回的结果
        public static <T> Result error(Integer code, String msg){
            Result resp = new Result(code,msg, null);
            return resp;
        }
    
    }
    
    • 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

    2.业务相关异常枚举类

    package com.liming.exception;
    
    /**
     * 这个枚举类中定义的都是跟业务有关的异常
     * @author 黎明
     * @date 2023/9/6 14:31
     * @version 1.0
     */
    public enum AppExceptionCodeMsg {
    
        // 业务操作错误定义
        INVALID_CODE(10000,"验证码无效"),
        USERNAME_NOT_EXISTS(10001,"用户名不存在"),
        USER_CREDIT_NOT_ENOUTH(10002,"用户积分不足"),
        ;
    
        // 错误码
        private Integer code ;
        // 错误消息
        private String msg ;
    
        AppExceptionCodeMsg(Integer code, String msg){
            this.code = code;
            this.msg = msg;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public String getMsg() {
            return 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

    3.自定义异常类

    package com.liming.exception;
    
    /**
     * 自定义异常类
     *
     * @author 黎明
     * @version 1.0
     * @date 2023/9/6 14:45
     */
    public class AppException extends RuntimeException {
    
        // 错误码
        private Integer code;
        // 异常的消息描述
        private String msg;
    
    
        public AppException(AppExceptionCodeMsg appExceptionCodeMsg) {
            super();
            this.code = appExceptionCodeMsg.getCode();
            this.msg = appExceptionCodeMsg.getMsg();
        }
    
        public AppException(Integer code, String msg) {
            super();
            this.code = code;
            this.msg = msg;
        }
    
        public int getCode() {
            return code;
        }
    
        public String getMsg() {
            return 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

    4.统一异常处理类

    package com.liming.exception;
    
    import com.liming.pojo.Result;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    /**
     * 全局统一异常处理类
     *
     * @author 黎明
     * @version 1.0
     * @date 2023/9/6 14:42
     */
    @ControllerAdvice
    public class GlobalExceptionHandler {
    
    
        @ExceptionHandler(value = {Exception.class})
        @ResponseBody
        public <T> Result<T> exceptionHandler(Exception e) {
            // 这里先判断拦截到的Exception是不是我们自定义的异常类型
            if (e instanceof AppException) {
                AppException appException = (AppException) e;
                return Result.error(appException.getCode(), appException.getMsg());
            }
    
            // 如果拦截的异常不是我们自定义的异常(例如:数据库主键冲突)
            return Result.error(500, "服务器端异常");
        }
    }
    
    • 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

    5.测试

    controller

    package com.liming.controller;
    
    import com.liming.exception.AppException;
    import com.liming.exception.AppExceptionCodeMsg;
    import com.liming.pojo.Result;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * @author 黎明
     * @version 1.0
     * @date 2023/9/6 14:53
     */
    @Api(tags = "统一异常处理接口")
    @RestController
    public class UnifiedExceptionController {
    
        @ApiOperation("异常测试")
        @GetMapping("/demo")
        public Result<String> demo1(String name) {
            if ("ok".equals(name)) {
                return Result.success("succ");
            }
            if ("err".equals(name)) {
                //抛自定义业务相关的异常
                throw new AppException(AppExceptionCodeMsg.USERNAME_NOT_EXISTS);
            }
            if ("errCode".equals(name)) {
                //抛自定义业务相关的异常
                throw new AppException(AppExceptionCodeMsg.INVALID_CODE);
            }
            if("notenough".equals(name)){
                //抛自定义业务相关的异常
                throw new AppException(AppExceptionCodeMsg.USER_CREDIT_NOT_ENOUTH);
            }
            if ("0".equals(name)) {
                // 抛出其他异常
                int i = 1 / 0;
            }
            return Result.success("操作成功");
        }
    
        @GetMapping("list")
        public Result<List> list(){
            List<String> list = Arrays.asList("zhangsan","lisi","wangwu");
    
            return Result.success(list);
        }
    }
    
    • 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

    结果展示
    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    【数据结构】顺序表+链表
    新手如何学电影解说剪辑全教程
    有 5nm 制程工艺的 MCU 吗?
    荧光染料AF488 DBCO, 5-isomer,AF488 二苯基环辛炔, 5-异构体
    面了 10 家大厂!我把问烂了的常见面试题总结了一下
    通过关键词合并2张excel表格——Python代码实现
    【vscode新建文件设置默认代码以C语言为例】
    《uni-app》一个非canvas的飞机对战小游戏实现-requestAnimationFrame详解
    Lingo软硬件划分 实例
    jQuery-tmpl 模板引擎使用方法说明
  • 原文地址:https://blog.csdn.net/weixin_46370595/article/details/132716257