美文网首页Java基础
全局异常捕获和统一Json数据结构返回

全局异常捕获和统一Json数据结构返回

作者: 小方块886 | 来源:发表于2021-02-20 15:06 被阅读0次

    1.定义实体

    1.1 错误码枚举类

    @Getter
    public enum ErrorEnum {
        // 数据操作错误定义
        SUCCESS(200, "成功"),
        NO_REQUEST(400,"请求无效"),
        NO_AUTH(401,"未授权:登录失败"),
        NO_PERMISSION(403,"你没得权限"),
        NOT_FOUND(404, "未找到该资源!"),
        INTERNAL_SERVER_ERROR(500, "服务器跑路了"),
        BUSINESS_ERROR(501, "客户端业务异常"),
        ;
        /** 错误码 */
        private Integer errorCode;
        /** 错误信息 */
        private String errorMsg;
        ErrorEnum(Integer errorCode, String errorMsg) {
            this.errorCode = errorCode;
            this.errorMsg = errorMsg;
        }
    

    1.2 统一返回Json实体类

    @Data
    public class Result<T> {
    
        //是否成功
        private Boolean success;
        //状态码
        private Integer code;
        //提示信息
        private String msg;
        //数据
        private T data;
        public Result() {
    
        }
        
        public Result(Boolean success, Integer code, String msg, T data) {
            this.success = success;
            this.code = code;
            this.msg = msg;
            this.data = data;
        }
        /** 业务成功返回业务代码,描述和返回的参数 */
        public static <T> Result<T> success(T data) {
            return new Result<T>(true,ErrorEnum.SUCCESS.getErrorCode(),ErrorEnum.SUCCESS.getErrorMsg(),data);
        }
    
        //自定义异常返回的结果
        public static Result defineError(DefinitionException de){
            Result result = new Result();
            result.setSuccess(false);
            result.setCode(de.getErrorCode());
            result.setMsg(de.getErrorMsg());
            result.setData(null);
            return result;
        }
        //其他异常处理方法返回的结果
        public static Result otherError(ErrorEnum errorEnum){
            Result result = new Result();
            result.setMsg(errorEnum.getErrorMsg());
            result.setCode(errorEnum.getErrorCode());
            result.setSuccess(false);
            result.setData(null);
            return result;
        }
    
    }
    

    1.3 自定义异常实体类(提供两个构造方法)

    @Data
    public class DefinitionException extends RuntimeException {
    
        protected Integer errorCode;
        protected String errorMsg;
    
        /**
         * 自定义错误码和错误描述
         * @param errorCode
         * @param errorMsg
         */
        public DefinitionException(Integer errorCode, String errorMsg) {
            this.errorCode = errorCode;
            this.errorMsg = errorMsg;
        }
    
        /**
         *  只指定错误描述,不指定错误码
         * @param errorMsg
         */
        public DefinitionException(String errorMsg) {
            this.errorCode = errorCode;
            this.errorMsg = errorMsg;
        }
    
    }
    
    

    1.4 在提供一个可以业务使用的业务异常类

    public class BusinessException extends DefinitionException {
    
        public BusinessException(Integer errorCode, String errorMsg) {
            super(errorCode, errorMsg);
        }
    
        public BusinessException(String errorMsg) {
            super(ErrorEnum.BUSINESS_ERROR.getErrorCode(),errorMsg);
        }
    
    }
    

    2.创建两个处理器

    2.1 全局异常处理器

    @RestControllerAdvice
    public class GlobalExceptionHandler {
    
        /**
         * 处理自定义异常
         *
         */
        @ExceptionHandler(value = DefinitionException.class)
        @ResponseBody
        public Result bizExceptionHandler(DefinitionException e) {
            return Result.defineError(e);
        }
    
        /**
         * 处理其他异常
         *
         */
        @ExceptionHandler(value = Exception.class)
        @ResponseBody
        public Result exceptionHandler( Exception e) {
            return Result.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);
        }
    

    2.2 统一Json返回处理器

    @RestControllerAdvice
    public class ResponseResultBodyAdvice implements ResponseBodyAdvice {
        @Override
        public boolean supports(MethodParameter methodParameter, Class aClass) {
            return true;
        }
    
        @Override
        public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
            if (body instanceof Result){
                return body;
            }
            return Result.success(body);
        }
    

    3.打包成start,这样其他项目直接引入pom就可以使用了

    3.1 创建一个配置类(将两个处理器交给spring管理)

    @Configuration
    public class StarterAutoConfigure {
    
        @Bean
        public GlobalExceptionHandler globalExceptionHandler(){
            return new GlobalExceptionHandler();
        }
    
        @Bean
        public ResponseResultBodyAdvice responseResultBodyAdvice(){
            return new ResponseResultBodyAdvice();
        }
    }
    

    3.2 需要在resources目录下新建一个META-INF的目录,并创建一个spring.factories配置文件,并写上下面的配置
    注:第一行是固定的,第二行是配置类的地址

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.example.springbootdemo02.StarterAutoConfigure
    

    4.大功告成,我们在另外个项目引入试一下

    4.1 测试一下正常返回

        @GetMapping("result")
        public User result(){
            User user = new User();
            user.setId(886);
            user.setName("张三");
            return user;
        }
    
      输出结果:
    {
        "success": true,
        "code": 200,
        "msg": "成功",
        "data": {
            "id": 886,
            "name": "张三"
        }
    }
    

    4.2 再试一下系统发生异常的情况

    @GetMapping("result")
       public User result(){
           User user = new User();
           user.setId(886);
           user.setName("张三");
           // 这里会报数学计算错误异常
           Integer a = 1/0;
           return user;
       }
    输出结果:
    {
       "success": false,
       "code": 500,
       "msg": "服务器跑路了",
       "data": null
    }
    

    4.3 试试业务异常的情况

    @GetMapping("result")
        public User result(){
            User user = new User();
            user.setId(886);
            user.setName("张三");
            if (user.getId() > 100){
                throw new BusinessException("年龄大于100啦...");
            }
            return user;
        }
    输出结果:
    {
        "success": false,
        "code": 501,
        "msg": "年龄大于100啦...",
        "data": null
    }
    

    相关文章

      网友评论

        本文标题:全局异常捕获和统一Json数据结构返回

        本文链接:https://www.haomeiwen.com/subject/nbotfltx.html