zoukankan      html  css  js  c++  java
  • 项目中Controller的全局异常处理类

    /**
     * 全局异常处理
     * 
     */
    @ControllerAdvice
    public class GlobalExceptionHandler {
    
        @ResponseBody
        @ExceptionHandler(value = ApiException.class)
        public CommonResult handle(ApiException e) {
            if (e.getErrorCode() != null) {
                return CommonResult.failed(e.getErrorCode());
            }
            return CommonResult.failed(e.getMessage());
        }
    
        @ResponseBody
        @ExceptionHandler(value = MethodArgumentNotValidException.class)
        public CommonResult handleValidException(MethodArgumentNotValidException e) {
            BindingResult bindingResult = e.getBindingResult();
            String message = null;
            if (bindingResult.hasErrors()) {
                FieldError fieldError = bindingResult.getFieldError();
                if (fieldError != null) {
                    message = fieldError.getField()+fieldError.getDefaultMessage();
                }
            }
            return CommonResult.validateFailed(message);
        }
    
        @ResponseBody
        @ExceptionHandler(value = BindException.class)
        public CommonResult handleValidException(BindException e) {
            BindingResult bindingResult = e.getBindingResult();
            String message = null;
            if (bindingResult.hasErrors()) {
                FieldError fieldError = bindingResult.getFieldError();
                if (fieldError != null) {
                    message = fieldError.getField()+fieldError.getDefaultMessage();
                }
            }
            return CommonResult.validateFailed(message);
        }
    }
    /**
     * 自定义API异常
     * 
     */
    public class ApiException extends RuntimeException {
        private IErrorCode errorCode;
    
        public ApiException(IErrorCode errorCode) {
            super(errorCode.getMessage());
            this.errorCode = errorCode;
        }
    
        public ApiException(String message) {
            super(message);
        }
    
        public ApiException(Throwable cause) {
            super(cause);
        }
    
        public ApiException(String message, Throwable cause) {
            super(message, cause);
        }
    
        public IErrorCode getErrorCode() {
            return errorCode;
        }
    }
    /**
     * 断言处理类,用于抛出各种API异常
     * 
     */
    public class Asserts {
        public static void fail(String message) {
            throw new ApiException(message);
        }
    
        public static void fail(IErrorCode errorCode) {
            throw new ApiException(errorCode);
        }
    }

    使用断言处理类来抛出各种异常

  • 相关阅读:
    初始化ArrayList的两种方法
    MySQL最大连接数设置
    页面按钮的语义和可访问性
    H5+App开发框架汇总
    JS使用模板快速填充HTML控件数据
    Meta标签中的format-detection属性及含义
    java中@Qualifier("string")是什么用法
    MySQL 当记录不存在时insert,当记录存在时update
    美国40岁以上的程序员在干啥
    老程序员都去哪了?
  • 原文地址:https://www.cnblogs.com/wk-missQ1/p/14630739.html
Copyright © 2011-2022 走看看