CodexBloom - Programming Q&A Platform

Tomcat 9 Error When Using Spring Boot with Custom Error Pages Not Displaying

👀 Views: 2 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-04
spring-boot tomcat error-handling Java

I'm running a Spring Boot application on Tomcat 9, and I've configured custom error pages for various HTTP status codes in my application. However, when a 404 error occurs, instead of displaying my custom error page, I'm getting the default Tomcat error page. I've verified that my error page configuration in `application.properties` is correct: ```properties server.error.whitelabel.enabled=false server.error.path=/error ``` And in my controller, I've set up a handler for 404 errors: ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(NoHandlerFoundException.class) public String handleNotFound() { return "error/404"; // points to src/main/resources/templates/error/404.html } } ``` I've also added the following in my `WebMvcConfigurer` to ensure the application throws `NoHandlerFoundException` instead of the default behavior: ```java @Override public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) { resolvers.add(new DefaultHandlerExceptionResolver() { @Override protected ModelAndView handleNoHandlerFoundException(NoHandlerFoundException ex, HttpServletRequest request, HttpServletResponse response) throws Exception { response.setStatus(HttpStatus.NOT_FOUND.value()); return new ModelAndView("error/404"); } }); } ``` Despite all this, when I navigate to a non-existent route, the default Tomcat error page still appears. I've tried cleaning and rebuilding my project as well as restarting the Tomcat server, but nothing seems to work. Is there something I might be missing in my configuration, or is there an additional step needed to ensure that custom error pages are displayed in Tomcat 9?