项目原来的处理方式
在我的项目中,我原本只使用了 @ExceptionHandler 注解,并通过继承的方式实现了统一异常处理。我创建了一个名为 BaseController 的父类,其中包含了以下代码:
public class BaseController {
@ExceptionHandler(Exception.class)
public RestResult handleException(Throwable e) {
//定义异常处理的响应数据模型
RestResult result = new RestResult();
result.setSuccess(false);
result.setData(e.getMessage());
result.setErrorCode(500);
e.printStackTrace();
return result;
}
}
然后,我让项目中需要统一异常管理的 @Controller 类都继承了 BaseController。例如,我有一个名为 UserController 的控制器:
@RequestMapping("/user")
public class UserController extends BaseController {
···
}
通过这种方式,我成功实现了统一异常处理,但是这种方法需要让所有的 @Controller 类都继承BaseController,有些繁琐。
使用@RestControllerAdvice和@ExceptionHandler组合
后来,我在网上找到了使用 @RestControllerAdvice 和 @ExceptionHandler 组合的方法。我创建了一个名为 GlobalExceptionHandler 的类,其中包含了以下代码:
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public RestResult handleException(Throwable e) {
//定义异常处理的响应数据模型
RestResult result = new RestResult();
result.setSuccess(false);
result.setData(e.getMessage());
result.setErrorCode(500);
e.printStackTrace();
return result;
}
}
遇到的问题描述
起初,我以为这种方法不会有什么问题。但是在测试过程中,我发现统一处理异常的方法一直没有被调用,即使我尝试了很多方法也没有解决。
直到我看到一个帖子提到,被 @RestControllerAdvice 标注的类必须单独放到一个文件夹中,我半信半疑地创建了一个名为 "a" 的文件夹,并将 GlobalExceptionHandler 类放进去后重新测试。令我没想到的是,异常发生时确实跳转到了统一处理的方法中。
然后,我又将文件夹的名字改为 "exceptionHandler" 后重新测试,结果又无法跳转到统一处理的方法中。
此时我已经发现了问题所在。由于我将控制器代码放在了名为 "controller" 的文件夹中,一开始我将 "GlobalExceptionHandler" 类放到名为 "a"的文件夹,它在 "controller" 的首字母 "c" 上面;后来我我将 "GlobalExceptionHandler" 类放到以 "e" 开头的文件夹,它在 "controller" 的首字母 "c" 下面。
因此,这可能是由于 Spring Bean 加载顺序的问题。
解决
只需要在GlobalExceptionHandler类中添加@Order注解,以确保它优先加载就没问题了。
@Order(9999)
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public RestResult handleException(Throwable e) {
//定义异常处理的响应数据模型
RestResult result = new RestResult();
result.setSuccess(false);
result.setData(e.getMessage());
result.setErrorCode(500);
e.printStackTrace();
return result;
}
}
添加了 @Order 注解后,重新进行测试,问题就得到了解决。
以上就是我遇到这个问题的解决过程,供参考。