@ExceptionHandler null pointer in console after spring-boot upgrade
I had a bit of luck today, so I upgraded from Spring to 1.5.1. When I run my JAR file everything seems fine at first.
However, when I make a call that is expected to return an error response (negative test - say "entry not found"), it returns a properly formatted JSON response as expected.
However, I also notice a giant stack trace in the console.
There is a WARN log that mentions a null pointer somewhere.
WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver -
Failed to invoke @ExceptionHandler method: protected org.springframework.http.ResponseEntity<?> com.sgb.AbstractBaseController.handle(com.sgb.panic.CustomSgbException,org.springframework.web.context.request.WebRequest)
java.lang.NullPointerException: null
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:384)
at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:59)
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136)
at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:74)
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1218)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1030)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:664)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
It worked fine before I updated to the latest Spring boot 1.5.3. When I switched to 1.5.1, the stack trace disappears. I switch to 1.5.3, it comes back.
What gives? Appreciate any pointers.
EDIT: When I change the Spring logging level to DEBUG, I see that immediately after the above stack trace is logged, it tries to call ExceptionHandler again and succeeds a second time, which explains why the correct error response came up despite on the stack trace.
Then the question arises:
o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Invoking @ExceptionHandler method
the first call to @ExceptionHandler method fails.
EDIT: This SO post also addresses this issue.
In my case, I have an AbstractBaseController class that all my controllers extend where all exception handling takes place, instead of having a separate ControllerAdvice for my rest endpoint.
I tried to create a separate ControllerAdvice to handle all exceptions. It works for all my custom exceptions, however when it encounters a validation error thrown by @Valid (JSR-303), it looks like it doesn't fire my custom ExceptionHandler in my ControllerAdvice for MethodArgumentNotValidException. This results in an empty 400 with no error message on failed JSR 303 validation.
So I need to move all exception handlers back to the superclass for controllers, which results in the original WARN message in the log. This is less than ideal.
source to share