Java SpringMVC中的嵌套异常处理
我得到以下错误: org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是java.lang.NullPointerException 为了在控制器中处理此问题,我使用了以下代码:Java SpringMVC中的嵌套异常处理,java,spring,exception-handling,Java,Spring,Exception Handling,我得到以下错误: org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是java.lang.NullPointerException 为了在控制器中处理此问题,我使用了以下代码: @ExceptionHandler(NestedServletException.class) public ModelAndView handleServletErrors(){ System.out.println("Servlet
@ExceptionHandler(NestedServletException.class)
public ModelAndView handleServletErrors(){
System.out.println("Servlet Exception is thrown");
ModelAndView mv = new ModelAndView("error");
mv.addObject("error", "Error encountered while processing reqeust.");
return mv;
}
但这不会处理上面抛出的异常。然而,如果我使用
NullPointerException
类而不是NestedServletException
,它就可以工作了。既然Spring抛出异常以响应NullPointerException
,那么它不应该由上面的代码处理吗?引用以下文档:
用于处理特定处理程序类和/或处理程序方法中异常的注释
此注释将允许方法处理处理程序方法引发的异常,即使用@RequestMapping
注释的方法。引述:
您可以使用@ExceptionHandler
方法来实现这一点。当在控制器中声明时,此类方法适用于该控制器(或其任何子类)的@RequestMapping
方法引发的异常。您还可以在@ControllerAdvice
类中声明@ExceptionHandler
方法,在这种情况下,它处理来自许多控制器的@RequestMapping
方法的异常
由于处理程序引发的异常是NullPointerException
,因此异常处理程序方法将处理该特定异常。它不会处理Spring用来封装servlet异常的泛型NestedServletException