Nested Exception Handling in Spring MVC

I get the following error:

org.springframework.web.util.NestedServletException: request processing failed; The nested exception is java.lang.NullPointerException

And to handle this in the controller, I used the following code:

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

But this does not handle the exception mentioned above. If I use the NullPointerExceptionclass instead NestedServletException, it works. Since Spring throws an exception in response to NullPointerException, shouldn't it be handled using the code above?

+4
source share
1 answer

Indication of documentation @ExceptionHandler:

/ .

, , , @RequestMapping. Spring :

@ExceptionHandler. , @RequestMapping ( ). @ExceptionHandler @ControllerAdvice, @RequestMapping .

, , NullPointerException, . NestedServletException, Spring .

+1

Source: https://habr.com/ru/post/1610172/


All Articles