简体   繁体   English

Spring如何管理ExceptionHandler优先级?

[英]How does spring manage ExceptionHandler priority?

Giving this controller 给这个控制器

@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";
}

Both ExceptionHandler match the IllegalArgumentException because it's a child of Exception class. 两个ExceptionHandler都匹配IllegalArgumentException因为它是Exception类的子级。

When I reach /test endpoint, the method handleIllegalException is called. 当我到达/test端点时,调用方法handleIllegalException If I throw a NullPointerException , the method handleException is called. 如果我抛出NullPointerException ,则调用方法handleException

How does spring knows that it should execute the handleIllegalException method and not the handleException method ? Spring如何知道它应该执行handleIllegalException方法而不是handleException方法? How does it manage the priority when multiple ExceptionHandler match an Exception ? 当多个ExceptionHandler匹配异常时,它如何管理优先级?

(I thought the order or the ExceptionHandler declarations was important, but even if I declare handleIllegalException before handleException , the result is the same) (我认为顺序或ExceptionHandler声明很重要,但即使我在handleException之前声明handleIllegalException ,结果也一样)

Spring MVC provides many different methods for Exception handling definitions. Spring MVC为异常处理定义提供了许多不同的方法。

In general, it will try to find the most "specific" exception handler registered to handle the exception. 通常,它将尝试查找为处理异常而注册的最“特定”的异常处理程序。 If there is no such a handler, it will try to check for the superclass of exception, maybe there is a handler for it, if it's not found as well, it will go one more level up and so on and so forth, from the most specific to the most general. 如果没有这样的处理程序,它将尝试检查异常的超类,也许它有一个处理程序,如果它也没有找到,它会更高级别等等等等,从最具体的一般。

If you want to see it in the code of Spring, an entry point to learn this topic would be: 如果你想在Spring的代码中看到它,学习这个主题的入口点将是:

org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver

This class resolves the exceptions registered through @ExceptionHandler methods out of the beans registered in the application context. 此类解析通过应用程序上下文中注册的bean之外的@ExceptionHandler方法注册的异常。 This class, in turn, uses the another class org.springframework.web.method.annotation.ExceptionHandlerMethodResolver which is responsible for mapping all the methods marked with @ExceptionHandler annotation. 反过来,这个类使用另一个类org.springframework.web.method.annotation.ExceptionHandlerMethodResolver ,它负责映射用@ExceptionHandler注释标记的所有方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM