【问题标题】:java - throwExceptionIfNoHandlerFound not worksjava - throwExceptionIfNoHandlerFound 不起作用
【发布时间】:2017-08-26 13:04:54
【问题描述】:

我正在尝试在使用 Java 配置完全配置的 Spring MVC 应用程序中使用 @ControllerAdvice 处理 404 错误。

这里有我的conf:

public class WebAppInitializer implements WebApplicationInitializer
{

    @Override
    public void onStartup(ServletContext container)
    {

        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
        dispatcherServlet.register(WebMvcConfig.class);

        dispatcherServlet.setServletContext(container);
        dispatcherServlet.refresh();

        CookieHelper cookie = (CookieHelper) dispatcherServlet.getBean("cookie");
        final Gson gson = (Gson) dispatcherServlet.getBean("gson");

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
        dispatcher.addMapping("/");
        dispatcher.setLoadOnStartup(1);
        dispatcher.setInitParameter("throwExceptionIfNoHandlerFound", "true");

        FilterRegistration.Dynamic filter = container.addFilter("BaseFilter", new BaseFilter(cookie, gson));
        filter.setInitParameter("forceEncoding", "true");
        filter.addMappingForUrlPatterns(null, true, "/coolers/*");
        filter.addMappingForUrlPatterns(null, true, "/hothouses/*");
        filter.addMappingForUrlPatterns(null, true, "/lang/*");
        filter.addMappingForUrlPatterns(null, true, "/organizations/*");
        filter.addMappingForUrlPatterns(null, true, "/reworks/*");
        filter.addMappingForUrlPatterns(null, true, "/select/*");
        filter.addMappingForUrlPatterns(null, true, "/volumes/*");
    }

}

还有我的 GlobalExceptionHandlerController:

@ControllerAdvice
public class GlobalExceptionHandlerController 
{
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String handle() {
      System.out.println("test test test test");
      return "error/index";
    }
} 

NoHandlerFoundException 没有触发?

【问题讨论】:

  • 您是否正在执行任何会导致 NoHandlerFoundException 的操作?
  • 即使线程较旧,遇到相同的情况,写下答案,以便它可以帮助其他人。

标签: java spring exception http-status-code-404


【解决方案1】:

我遇到了同样的问题,已经解决了。下面给出了解决相同问题的步骤。

  1. 创建一个带有@ControllerAdvice注解的类GlobalExceptionHandler
@ControllerAdvice
public class GlobalExceptionHandler 
{
    @ExceptionHandler(NoHandlerFoundException.class)
    public String handleNotFoundError(Exception ex) 
    {
        return "redirect:/yourCustom404page";
    }
}
  1. 默认情况下,当页面/资源不存在时,servlet 容器将呈现默认的 404 页面。如果您想要自定义 404 响应,那么您需要告诉 DispatcherServlet 如果未找到处理程序则抛出异常。我们可以通过将throwExceptionIfNoHandlerFound servlet 初始化参数设置为true 来做到这一点

一个。如果基于spring-mvc java的配置是

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
    ...

    @Override
    protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) 
    {
        final DispatcherServlet servlet = (DispatcherServlet) super.createDispatcherServlet(servletAppContext);
        servlet.setThrowExceptionIfNoHandlerFound(true);
        return servlet;
    }

}

b.如果基于 spring-mvc xml 的配置,请像这样初始化调度程序 servlet

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>throwExceptionIfNoHandlerFound</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

c。如果 spring-boot
spring.resources.add-mappings=false 在您的 application.properties 或 yaml 文件中。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2014-11-23
    • 2016-08-28
    • 2012-07-21
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多