Spring 3 - Create ExceptionHandler for NoSuchRequestHandlingMethodException

前端 未结 3 767
一整个雨季
一整个雨季 2021-01-31 09:18

Using Spring 3, I like to create an exception handler using the ExceptionHandler annotation that will handle \"no page found (404)\" requests.

3条回答
  •  轮回少年
    2021-01-31 09:54

    @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.

    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.

    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.

提交回复
热议问题