给这个控制器
@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方法.如果抛出NullPointerException,则将调用handleException方法.
spring如何知道它应该执行handleIllegalException方法而不是handleException方法?当多个ExceptionHandler匹配一个Exception时,它如何管理优先级?
(我认为顺序或ExceptionHandler声明很重要,但是即使我在handleException之前声明handleIllegalException,结果也是一样的)