2
votes

I´m having a rather strange issue with the embedded tomcat that comes with spring-boot, specifically with it´s error handling.

Basicly, I´m trying to handle all exceptions via a custom handler. Long story short: It works for everything except 404. Are those handled via any other mechanism and not Exceptions?

All other status codes I tested with call my custom Resolver, only 404s present me with the default Tomcat 404 page. More specifically, 404s thrown outside of a controller, e.g. a request for /teXst if there are only controllers for /test registered. On such exceptions I don´t get my Resolver called and only see the default Tomcat HTTP Status 404 page.

Any idea what might get in my way? Here is how I set up my Resolver

public class GlobalExceptionResolver extends AbstractHandlerExceptionResolver {
        @Override
        public int getOrder() {
            return Integer.MIN_VALUE;
        }

        @Override
        protected boolean shouldApplyTo(HttpServletRequest request, Object handler) {
            return true;
        }

        @Override
        protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
     // my stuff ending with returning an instance of ModelAndView
    }

To get all default Spring-Boot Error handling out of the way I annotate with

@EnableAutoConfiguration(exclude = org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration.class)

which leaves me with the default Tomcat Error pages for 404, 500 and so on, and finally set up my custom handler with

@Bean
public WebMvcConfigurerAdapter adapter() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
            exceptionResolvers.add(new GlobalExceptionResolver());
        }

As said, this setup works fine for all exceptions except HTTP status 404. Any idea what mechanism creates these pages and why my exception resolver is not called? This is on Spring Boot 1.2.5.RELEASE using Spring MVC 4.1.7.RELEASE and embedded Tomcat 8.0.23

1

1 Answers

1
votes

As it turns out, i found the answer. It is not possible via Spring Configuration, but you can create your own ServletDispatcher and configure it to trow an exception as described here: How to configure default DispatcherServlet?

Then my above code catches 404s just fine