Spring 3 - 为NoSuchRequestHandlingMethodException创建ExceptionHandler [英] Spring 3 - Create ExceptionHandler for NoSuchRequestHandlingMethodException

查看:194
本文介绍了Spring 3 - 为NoSuchRequestHandlingMethodException创建ExceptionHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring 3,我喜欢使用 ExceptionHandler 注释创建一个异常处理程序,该注解将处理找不到页面(404)请求。我使用以下代码来做到这一点。但是当我指向一个不存在的URL时,正在调用Spring定义的默认异常处理程序。

Using Spring 3, I like to create an exception handler using the ExceptionHandler annotation that will handle "no page found (404)" requests. I am using the following code to do this. But when I point to a URL that does not exist, the default exception handler defined by Spring is being invoked.

可能是我正在处理 NoSuchRequestHandlingMethodException 异常。如果是,那么我应该注册什么样的例外?

It might be that I'm handling the NoSuchRequestHandlingMethodException exception. If it is, then what exception should I be registering?

请你看看下面的代码,看看我在做错什么?

Will you please take a look at the following code and see what I'm doing wrong?

注意:如果我将 @ExceptionHandler 中的异常更改为 NullPointerException ,并创建一个 RequestMapping 抛出空指针,这将工作。

NOTE: If I change the exception in the @ExceptionHandler to NullPointerException and create a RequestMapping to throw null pointer, that will work.

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.web.servlet.ModelAndView;

    @Controller
    public class GeneralHandler {
      private final Log logger = LogFactory.getLog(getClass());

      @ExceptionHandler(NoSuchRequestHandlingMethodException.class)
      public ModelAndView handleException (NoSuchRequestHandlingMethodException ex) {
        ModelAndView mav = new ModelAndView();
        logger.error("Exception found: " + ex);
        return mav;
      }
    }


推荐答案

code> @ExceptionHandler -annotated方法在同一个类的 @RequestMapping 方法引发异常时被调用。所以当你添加了抛出 NullPointerException 的映射,这是有效的,因为映射的方法和异常处理程序在同一个类中。

@ExceptionHandler-annotated methods are invoked when a @RequestMapping method on that same class throws an exception. So when you added the mapping which threw the NullPointerException, that worked, since the mapped method and exception handler were together in the same class.

当没有找到映射时,Spring无法将 NoSuchRequestHandlingMethodException 与您的 @ExceptionHandler ,因为它没有得到一个匹配的请求到一个处理程序方法。这不是在文档中明确提到,而是我观察到的行为。

When no mapping is found, Spring has no way of associating the NoSuchRequestHandlingMethodException with your @ExceptionHandler, because it didn't get as far as matching the request to a handler method. This isn't mentioned explicitly in the docs, but is the behaviour I've observed.

如果要特别处理这个异常,你将不得不使用更一般的 HandlerExceptionResolver 方法,而不是更专门的 @ExceptionHandler 技术。

If you want to handle this exception specially, you're going to have to use the more general HandlerExceptionResolver approach, rather than the more specialised @ExceptionHandler technique.

这篇关于Spring 3 - 为NoSuchRequestHandlingMethodException创建ExceptionHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆