Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3.2 servlet 3.0 Java Config (no web.xml) how to create custom 404 handler

What options do I have to catch and handle 404 without using web.xml. This is how I define my Servlet:

@Order(1)
public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    // 10MB
    private static final int MAX_UPLOAD_SIZE_IN_MB = 10 * 1024 * 1024;

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { RootConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebConfiguration.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] { new SiteMeshFilter() };
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/tmp", MAX_UPLOAD_SIZE_IN_MB, MAX_UPLOAD_SIZE_IN_MB * 2,
                MAX_UPLOAD_SIZE_IN_MB / 2);
        registration.setMultipartConfig(multipartConfigElement);
    }

}

My application is all configured in java code, @ControllerAdvice works fine with all other exceptions like:

BindException.class, MethodArgumentNotValidException.class, MissingServletRequestParameterException.class,
MissingServletRequestPartException.class, HttpMessageNotReadableException.class, TypeMismatchException.class

however

@ControllerAdvice
public class SomeClass {
...
    @ExceptionHandler(value = { ResourceNotFoundException.class, NoSuchRequestHandlingMethodException.class })
    public ModelAndView handle404() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/errors/404");
        modelAndView.addObject("exception", "404 NOT FOUND!");
        return modelAndView;
    }
...
}

does NOT catch 404 I have also tried HttpStatus... without any luck

when I call /someUrlThatDoesNotExist :

[DEBUG] 2014-01-24 10:03:16,639 org.springframework.security.web.FilterChainProxy doFilter - /someUrlThatDoesNotExist reached end of additional filter chain; proceeding with original chain
[DEBUG] 2014-01-24 10:03:16,640 org.springframework.web.servlet.DispatcherServlet doService - DispatcherServlet with name 'dispatcher' processing GET request for [/someUrlThatDoesNotExist]
[DEBUG] 2014-01-24 10:03:16,641 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping getHandlerInternal - Looking up handler method for path /someUrlThatDoesNotExist
[DEBUG] 2014-01-24 10:03:16,662 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping getHandlerInternal - Did not find handler method for [/someUrlThatDoesNotExist]
[WARN] 2014-01-24 10:03:16,663 org.springframework.web.servlet.PageNotFound noHandlerFound - No mapping found for HTTP request with URI [/someUrlThatDoesNotExist] in DispatcherServlet with name 'dispatcher'
[DEBUG] 2014-01-24 10:03:16,665 org.springframework.web.servlet.DispatcherServlet processRequest - Successfully completed request

but when I try inspect below point-cuts using AOP:

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpRequestMethodNotSupported(..)
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleNoSuchRequestHandlingMethod(..)

they never get caught..

like image 907
Marcin Wasiluk Avatar asked Jan 24 '14 10:01

Marcin Wasiluk


1 Answers

I believe you need to configure your DispatcherServlet to throw an exception if no handler is found

dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

and then have your @ControllerAdvice declare an @ExceptionHandler for an exception of type NoHandlerFoundException.

like image 122
Sotirios Delimanolis Avatar answered Oct 11 '22 13:10

Sotirios Delimanolis