给这个控制器
@GetMapping("/test")
@ResponseBody
public String test() {
if (!false) {
throw new IllegalArgumentException();
}
return "blank";
}
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
@ResponseBody
public String handleException(Exception e) {
return "Exception handler";
}
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
public String handleIllegalException(IllegalArgumentException e) {
return "IllegalArgumentException handler";
}
两个ExceptionHandler都匹配,IllegalArgumentException
因为它是Exception
类的孩子。
当我到达/test
端点时,该方法handleIllegalException
被调用。如果我抛出 a NullPointerException
,handleException
则调用该方法。
spring如何知道它应该执行handleIllegalException
方法而不是handleException
方法?当多个ExceptionHandler匹配一个 Exception时,它如何管理优先级?
(我认为顺序或ExceptionHandler声明很重要,但即使我handleIllegalException
之前声明handleException
,结果也是一样的)