簡體   English   中英

Spring ControllerAdvice ExceptionHandler不起作用

[英]Spring ControllerAdvice ExceptionHandler does not work

我正在嘗試使用ExceptionHandler攔截tomcat默認的404頁面。 我要做的就是在404情況下顯示索引頁。

@ControllerAdvice
public class AdvisorController {

@ExceptionHandler(NoHandlerFoundException.class)
public String handle404(Exception ex) {
    return "index";
}
}

但是很遺憾,我只能看到tomcat默認的404頁面。

我的網絡初始化程序如下所示:

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("/");
}
}

請幫忙。 我究竟做錯了什么?

我發現了問題:

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

刪除后,一切正常。 也許會幫助某人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM