HOWTO handle 404 exceptions globally using Spring MVC configured using Java based Annotations
By default, the DispatcherServlet
does not throw a NoHandlerFoundException
. You need to enable that.
The AbstractAnnotationConfigDispatcherServletInitializer
should let you override how the DispatcherServlet
is created. Do that and call
DispatcherServlet dispatcherServlet = ...; // might get it from super implementation
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
Enable DispatcherServlet throw a NoHandlerFoundException through web.xml configuartion.
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
Instead overriding registerDispatcherServlet
one can override the createDispatcherServlet
method as follows.
@Override
protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
DispatcherServlet ds = new DispatcherServlet(servletAppContext);
ds.setThrowExceptionIfNoHandlerFound(true);
return ds;
}