I am running Spring 4.3.7 in combination with Spring Security 4.2.2 and configured it using Java config, so no web.xml. Now I want to get a custom 404 page and the way to go seems to be overwriting the createDispatcherServlet Method as described here and here to throw an exception if a page cannot be resolved. I want to use a SimpleMappingExceptionResolver to handle it and return my custom 404 page, but throwing the exception does not even work for me yet. I hope the configurations given below are enough.
MvcWebApplicationInitializer.java
public class MvcWebApplicationInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("resourceUrlEncodingFilter",
new ResourceUrlEncodingFilter());
filterRegistration.setInitParameter("encoding", "UTF-8");
filterRegistration.setInitParameter("forceEncoding", "true");
filterRegistration.addMappingForUrlPatterns(null, true, "/*");
}
@Override
protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext)
{
DispatcherServlet dispatcherServlet = (DispatcherServlet) super.createDispatcherServlet(servletAppContext);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
return dispatcherServlet;
}
}
AppConfig.java
@EnableWebMvc
@Configuration
@ComponentScan(...)
@EnableTransactionManagement
public class AppConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
[...]
}
@Autowired
private RequestMappingHandlerAdapter requestMappingHandlerAdapter;
@PostConstruct
public void init() {
requestMappingHandlerAdapter.setIgnoreDefaultModelOnRedirect(true);
}
@Bean(name = "simpleMappingExceptionResolver")
public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() {
SimpleMappingExceptionResolver exceptionResolver = new MappingExceptionResolver();
Properties mappings = new Properties();
mappings.setProperty("UserNotAccessibleException", "access");
mappings.setProperty("VersionsModelCreationException", "versions");
exceptionResolver.setExceptionMappings(mappings);
exceptionResolver.setDefaultErrorView("index");
exceptionResolver.setExceptionAttribute("exception");
return exceptionResolver;
}
[...]
}
I still only get a
WARN in DispatcherServlet.java:noHandlerFound:1172: No mapping found for HTTP request with URI [/root/notexisting] in DispatcherServlet with name 'dispatcher'
instead of an exception that I might handle with my SimpleMappingExceptionResolver. Any advice?