I have a Spring Boot application running in a stand alone Tomcat container - I have slowly been making progress in getting the global error handling to work (custom 404 pages etc), and the ErrorPageFilter
class is now catching the error, but it throws a NullPointerException
trying to forward the request to the ErrorPage
.
The stack trace is as follows:
java.lang.NullPointerException
org.springframework.boot.context.web.ErrorPageFilter.handleErrorStatus(ErrorPageFilter.java:141)
org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:112)
org.springframework.boot.context.web.ErrorPageFilter.access$000(ErrorPageFilter.java:59)
org.springframework.boot.context.web.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:101)
Looking at the source for ErrorPageFilter
on the version I have, it is failing trying to match the RequestDispatcher
for the error path:
private void handleErrorStatus(HttpServletRequest request,
HttpServletResponse response, int status, String message)
throws ServletException, IOException {
if (response.isCommitted()) {
handleCommittedResponse(request, null);
return;
}
String errorPath = getErrorPath(this.statuses, status);
if (errorPath == null) {
response.sendError(status, message);
return;
}
response.setStatus(status);
setErrorAttributes(request, status, message);
request.getRequestDispatcher(errorPath).forward(request, response);
(the last line is the NPE)
I have configured my error handling as follows, the error config:
@Configuration
class ErrorConfiguration implements EmbeddedServletContainerCustomizer {
@Override public void customize( ConfigurableEmbeddedServletContainer container ) {
container.addErrorPages(new ErrorPage( HttpStatus.NOT_FOUND, "/errors/404" ))
container.addErrorPages(new ErrorPage( HttpStatus.INTERNAL_SERVER_ERROR, "/errors/500" ))
}
}
Which seems to be ok - those error pages are registered and the ErrorPageFilter
is kicking in.
I have tried registering the path "/errors/404" as both a standard view-controller and as a @Controller
request mapping (also note, if I just go directly to /errors/404 in the browser the url is resolved and the page is displayed)
Can anyone shed any light on this?