zoukankan      html  css  js  c++  java
  • spring boot自定义异常与统一异常处理

    spring boot自定义异常与统一异常处理

    1.定义自定义异常类

    例:

    public class AppException extends RuntimeException {
    
        private final String code;
        private final String message;
    
        /**
         * @description 根据异常码、异常信息和异常包装统一异常
         * @params [code, message, throwable]
         * @return
         */
        public AppException(String code, String message, Throwable throwable) {
            super(message, throwable);
            this.code = code;
            this.message = message;
        }
    
        /**
         * @description 根据自定义异常枚举类包装统一异常
         * @params [exceptionCode]
         * @return
         */
        public AppException(MyExceptionCode exceptionCode) {
            super(exceptionCode.getMessage());
            this.code = exceptionCode.getCode();
            this.message = exceptionCode.getMessage();
        }
    
        /**
         * @description 根据自定义异常枚举类和异常包装统一异常
         * @params [exceptionCode, throwable]
         * @return
         */
        public AppException(MyExceptionCode exceptionCode, Throwable throwable) {
            super(exceptionCode.getMessage(), throwable);
            this.code = exceptionCode.getCode();
            this.message = exceptionCode.getMessage();
        }
    
        public String getCode() {
            return code;
        }
    
        @Override
        public String getMessage() {
            return message;
        }
    }
    

    例:

    public class BusinessException extends AppException {
    
        /**
         * @description 根据异常码、异常信息和异常包装统一异常
         * @params [code, message, throwable]
         * @return
         */
        public BusinessException(String code, String message, Throwable throwable) {
            super(code, message, throwable);
        }
    
        /**
         * @description 根据自定义异常枚举类包装统一异常
         * @params [exceptionCode]
         * @return
         */
        public BusinessException(MyExceptionCode exceptionCode) {
            super(exceptionCode);
        }
    
        /**
         * @description 根据自定义异常枚举类和异常包装统一异常
         * @params [exceptionCode, throwable]
         * @return
         */
        public BusinessException(MyExceptionCode exceptionCode, Throwable throwable) {
            super(exceptionCode, throwable);
        }
    
    }
    

    2.定义异常码

    例:

    @Getter
    @AllArgsConstructor
    public enum SystemCode implements MyExceptionCode {
    
        /**
         * SYSTEM_SUCCESS
         */
        SYSTEM_SUCCESS("0", "成功."),
    
        /**
         * XX_EXCEPTION
         */
        XX_EXCEPTION("100101", "XX_EXCEPTION.")
        ;
    
        private String code;
        private String message;
    
    }
    

    3.定义全局异常捕获类

    例:

    @Slf4j
    @ControllerAdvice
    public class GlobalExceptionResolver {
    
        public static final String ERROR_STRING = "{}:code:{} # message:{}";
    
        /**
         * @description 处理自定义异常AppException
         * @params [e]
         * @return boss.xtrain.log.exception.common.CommonResponse
         */
        @ExceptionHandler(value = AppException.class)
        @ResponseBody
        public CommonResponse handleAppException(AppException e) {
            String type = "APP异常";
            log.error(ERROR_STRING, type, e.getCode(), e.getMessage(), e);
            return CommonResponse.error(e.getMessage(), e.getCode());
        }
    
        /**
         * @description 处理参数校验异常MethodArgumentNotValidException
         * @params [e]
         * @return boss.xtrain.log.exception.common.CommonResponse
         */
        @ExceptionHandler(value = MethodArgumentNotValidException.class)
        @ResponseBody
        public CommonResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
            String type = "参数校验异常";
            StringBuilder message = new StringBuilder();
            for (FieldError error : e.getBindingResult().getFieldErrors()) {
                message.append(error.getDefaultMessage()).append(";");
            }
            log.error(ERROR_STRING, type, "150101", e.getMessage(), e);
            return CommonResponse.error(message.toString(), "150101");
        }
    
        /**
         * @description 处理服务调用异常Exception
         * @params [e]
         * @return boss.xtrain.log.exception.common.CommonResponse
         */
        @ExceptionHandler(value = ClientException.class)
        @ResponseBody
        public CommonResponse handleClientException(ClientException e) {
            String type = "服务间调用异常";
            log.error(ERROR_STRING, type, "150102", e.toString(), e);
            return CommonResponse.error(type, "150102");
        }
    
    }
    

    4.使用

    例:

        try {
            row = paperDao.save(paperEntity);
        } catch (Exception e) {
            throw new BusinessException(SystemCode.XX_EXCEPTION, e);
        }
    
  • 相关阅读:
    asp.net中,<%#%>,<%=%>和<%%>分别是什么意思,有什么区别
    解决IE11下载文件 文件名乱码问题
    gridview DataFormatString 属性设置须知
    SQL Server 2008 Windows身份验证改为混合模式身份验证 及修改sa密码
    如何解决IIS7上传文件大小限制,.NET 上传文件后 找不到目录解决
    windows server 2008 r2, 每隔一段时间自动关机
    思科DCHP解决方案
    2016.10.01
    oracle中,拼接的字符串给游标赋值
    IoDH 实现的单例模式
  • 原文地址:https://www.cnblogs.com/hujh/p/13781985.html
Copyright © 2011-2022 走看看