HandlerExceptionResolver

HandlerExceptionResolver

本文介绍了如何配置 Spring 4.0.5 以使用 @ExceptionHandler 而不是 ExceptionResolver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在特定控制器上添加了一个 @ExceptionHandler 方法.

I have added an @ExceptionHandler method on a specific controller.

@ExceptionHandler(NullPointerException.class)
@ResponseStatus(HttpStatus.Not_FOUND)
public @ResponseBody doSomeThing(NullPointerException e) {...}

问题是:我也有一个自定义

Problem is : I have also an custom

org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver

在我的 Rest 应用程序(旧版)中定义为 @Component.

defined as a @Component in my Rest Application (Legacy).

现在我的特定控制器中的 @ExceptionHandler 永远不会被调用.我需要支持这两种异常处理策略.

Now the @ExceptionHandler in my specific controller never gets called. I need to support both Exception Handling strategies.

如何配置 spring 4.0.5 以便调用控制器中的@ExceptionHandler?

How can I configure spring 4.0.5 so that my @ExceptionHandler in my Controller gets called?

推荐答案

DispatcherServlet 默认会加载和注册 ApplicationContext 中的所有 HandlerExceptionResolver 实现code> 来帮助处理来自 Controllers 的异常.您会注意到AbstractHandlerExceptionResolver 类实现了Ordered.大多数 HandlerExceptionResolver 实现都扩展了这个类,并且 DispatcherServlet 使用它来对 HandlerExceptionResolver 实现列表进行排序,以处理您的 抛出的异常控制器.

The DispatcherServlet by default will load and register all HandlerExceptionResolver implementations within the ApplicationContext to help with processing Exceptions from your Controllers. You'll notice that AbstractHandlerExceptionResolver class implements Ordered. Most HandlerExceptionResolver implementations extend this class and DispatcherServlet uses this to order the list of HandlerExceptionResolver implementations when it comes to handling Exceptions thrown by your Controllers.

正如您可能怀疑的那样,对 Controllers 上的 @ExceptionHandler 注释的处理也由 HandlerExceptionResolver 实现处理.准确地说:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver

As you might suspect, the processing of @ExceptionHandler annotations on your Controllers is also handled by a HandlerExceptionResolver implementation. To be precise: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver

我建议在您的情况下发生的是 DispatcherServletHandlerExceptionResolver 类型导致在 AnnotationMethodHandlerExceptionResolver 之前调用您的实现code> 意味着你的 @ExceptionHandler 注释被有效地忽略了.

What I would suggest is happening in your case is that the sort of HandlerExceptionResolver by the DispatcherServlet is causing your implementation to be invoked before the AnnotationMethodHandlerExceptionResolver meaning that your @ExceptionHandler annotations are effectively ignored.

为了解决这个问题,我们需要做两件事:

To counteract this, we need to do two things:

  1. 直接将 AnnotationMethodHandlerExceptionResolver 注册为 ApplicationContext
  2. 中的 bean
  3. 将此 bean 的 Order 值设置为大于自定义 HandlerExceptionResolver 实现的值
  1. Directly register the AnnotationMethodHandlerExceptionResolver as a bean within your ApplicationContext
  2. Set the Order value of this bean to be greater than that of your custom HandlerExceptionResolver implementation

这意味着在为您的 Controllers 处理 Exceptions 时,Spring 将首先查看 AnnotationMethodHandlerExceptionResolver 是否可以处理异常,如果没有,请回退到您的自定义 HandlerExceptionResolver 实现.

This will mean that when it comes to handling Exceptions for your Controllers, Spring will first see if the AnnotationMethodHandlerExceptionResolver can handle the Exception, and if not, fall back to your custom HandlerExceptionResolver implementation.

作为在 HandlerExceptionResolver 上设置顺序的示例(假设您在某处使用 XML 配置):

As an example of the setting the order on a HandlerExceptionResolver (assuming you're using XML configuration somewhere):

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver" p:order="1" />

您可能还想查看默认使用 DispatcherServlet 注册的 HandlerExceptionResolver 实现列表,并确定最适合您的应用程序的顺序.

You may also want to take a look at list of HandlerExceptionResolver implementations registered with DispatcherServlet by default and decide upon the order that most suits your application.

这篇关于如何配置 Spring 4.0.5 以使用 @ExceptionHandler 而不是 ExceptionResolver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 10:59