I'm trying to intercept tomcat default 404 page using ExceptionHandler. All I want to do is showing the index page in case of 404.
@ControllerAdvice
public class AdvisorController {
@ExceptionHandler(NoHandlerFoundException.class)
public String handle404(Exception ex) {
return "index";
}
}
But unfortunately I can see only tomcat default 404 page.
My web initializer looks like this:
public class CRManWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
//ctx.register(CRManMVCConfig.class);
ctx.register(CRManMVCConfig.class, CRManJPAConfig.class, CRManSecurityConfig.class);
ctx.setServletContext(servletContext);
servletContext.addListener(new ContextLoaderListener(ctx));
DispatcherServlet dispatcherServlet = new DispatcherServlet(ctx);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
Dynamic registration = servletContext.addServlet("dispatcher", dispatcherServlet);
registration.setLoadOnStartup(1);
registration.addMapping("/");
}
}
Please help. What am I doing wrong?
I found the problem:
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
After removing everything is working fine. Maybe it will help somebody
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.