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);
        }
    }

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

  • 相关阅读:
    C# Net 合并int集合为字符串,如:输入1,2,3,4,8 输出1~4,8
    sql server 安装出现需要sqlncli.msi文件,错误为 microsoft sql server 2012 native client
    C# Form 实现桌面弹幕
    C# Net 去除图片白边
    SQL common keywords examples and tricks
    Excel formula and tricks
    HIghcharts cheatsheet
    CSS common keywords examples and tricks
    小白终于弄懂了:c#从async/await到Task再到Thread
    LeetCode 2: single-number II
  • 原文地址:https://www.cnblogs.com/wk-missQ1/p/14630739.html
Copyright © 2011-2022 走看看