SpringBoot -- SpringBoot的全局异常处理

@ControllerAdvice
public class AppControllerAdvice {

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public JsonResponse errorHandler(Exception ex) {
        CommonLogger.error("","统一异常处理","系统异常",ex);
        if(ex instanceof HttpRequestMethodNotSupportedException){
            return JsonResponse.fail("405:请求方式不支持");
        }else if(ex instanceof NoHandlerFoundException){
            return JsonResponse.fail("404:没有找到访问资源");
        }else {
            return JsonResponse.fail("500:请求异常,请联系管理员");
        }
    }

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public JsonResponse bsErrorHandler(BusinessException ex) {
        CommonLogger.error("","统一异常处理","业务异常",ex);
        return JsonResponse.fail();
    }


}
发布了66 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Aeve_imp/article/details/98048814