• spring Cloud 全局异常捕获


    通过@RestControllerAdvice来处理。

    @RestControllerAdvice
    public class GlobalExceptionAdvice {
     
        @ExceptionHandler(value = Exception.class)
        public ApiResult<String> handlerAdException(HttpServletRequest request, Exception ex) {
            ApiResult<String> response = new ApiResult<>();
            response.setCodeToError(ex.getMessage());
            return response;
        }
    }
    ApiResult 是我自己包装的返回结果
    public class ApiResult<T> {
        public static final String FAIL_CODE = "0";
        public static final String SUC_CODE = "1";
        public static final String ERROR_CODE = "2";
        public static final String SUC_MESSAGE = "Operate successfully";
        public static final String FAIL_MESSAGE = "Operation failure";
        public static final String ERROR_MESSAGE = "System Error";
        public static final String NOACCESS_MESSAGE = "No permission to access this page.";
     
        private String code = FAIL_CODE;
        private String message = FAIL_MESSAGE;
        private T data;
     
        public String getCode() {
            return code;
        }
     
        public void setCode(String code) {
            this.code = code;
        }
     
        public void setCode(String code, String message) {
            this.code = code;
            this.message = message;
        }
     
        public void setCodeToSuccessed() {
            this.code = SUC_CODE;
            this.message = SUC_MESSAGE;
        }
        public void setCodeToSuccessed(T data) {
            this.data = data;
            this.code = SUC_CODE;
            this.message = SUC_MESSAGE;
        }
        
        public void setCodeToError(String message) {
            this.code = ERROR_CODE;
            this.message = message;
        }
        
        public void setCodeToError() {
            this.code = ERROR_CODE;
            this.message = ERROR_MESSAGE;
        }
     
        public void setCodeToFail(String message) {
            this.code = ERROR_CODE;
            this.message = message;
        }
     
        public void setCodeToFail() {
            this.code = FAIL_CODE;
            this.message = FAIL_MESSAGE;
        }
        
        public void setCodeByNoAccess() {
            this.code = FAIL_CODE;
            this.message = NOACCESS_MESSAGE;
        }
     
        public String getMessage() {
            return message;
        }
     
        public void setMessage(String message) {
            this.message = message;
        }
        
        public boolean isSuccess() {
            return SUC_CODE.equals(code);
        }
     
        public T getData() {
            return data;
        }
     
        public void setData(T data) {
            this.data = data;
        }
        
        
        
    }
  • 相关阅读:
    C语言堆栈入门——堆和栈的区别
    Unity时钟定时器插件——Vision Timer源码分析之一
    UNITY3D 2D物流流体插件下载|Liquid Physics 2D
    Unity3d插件Master Audio AAA Sound v3.5
    游戏行业的女性拥有强大的新盟友:Facebook
    Unity游戏设计与实现 南梦宫一线程序员的开发实例
    Unity4.6证书激活问题
    里诺全系列注册机+暗桩patch
    冰点还原8.53破解版
    NSE: known a priori estimate
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/14894856.html
Copyright © 2020-2023  润新知