【问题标题】:Spring 3.2 servlet 3.0 Java Config (no web.xml) how to create custom 404 handlerSpring 3.2 servlet 3.0 Java Config(无 web.xml)如何创建自定义 404 处理程序
【发布时间】:2014-02-15 07:19:30
【问题描述】:

在不使用web.xml 的情况下,我必须捕获和处理 404 的哪些选项。这就是我定义我的 Servlet 的方式:

@Order(1)
public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    // 10MB
    private static final int MAX_UPLOAD_SIZE_IN_MB = 10 * 1024 * 1024;

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { RootConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebConfiguration.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] { new SiteMeshFilter() };
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/tmp", MAX_UPLOAD_SIZE_IN_MB, MAX_UPLOAD_SIZE_IN_MB * 2,
                MAX_UPLOAD_SIZE_IN_MB / 2);
        registration.setMultipartConfig(multipartConfigElement);
    }

}

我的应用程序都是用 java 代码配置的,@ControllerAdvice 在所有其他例外情况下都能正常工作,例如:

BindException.class, MethodArgumentNotValidException.class, MissingServletRequestParameterException.class,
MissingServletRequestPartException.class, HttpMessageNotReadableException.class, TypeMismatchException.class

然而

@ControllerAdvice
public class SomeClass {
...
    @ExceptionHandler(value = { ResourceNotFoundException.class, NoSuchRequestHandlingMethodException.class })
    public ModelAndView handle404() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/errors/404");
        modelAndView.addObject("exception", "404 NOT FOUND!");
        return modelAndView;
    }
...
}

不捕获 404 我也试过 HttpStatus... 没有任何运气

当我调用 /someUrlThatDoesNotExist 时:

[DEBUG] 2014-01-24 10:03:16,639 org.springframework.security.web.FilterChainProxy doFilter - /someUrlThatDoesNotExist reached end of additional filter chain; proceeding with original chain
[DEBUG] 2014-01-24 10:03:16,640 org.springframework.web.servlet.DispatcherServlet doService - DispatcherServlet with name 'dispatcher' processing GET request for [/someUrlThatDoesNotExist]
[DEBUG] 2014-01-24 10:03:16,641 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping getHandlerInternal - Looking up handler method for path /someUrlThatDoesNotExist
[DEBUG] 2014-01-24 10:03:16,662 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping getHandlerInternal - Did not find handler method for [/someUrlThatDoesNotExist]
[WARN] 2014-01-24 10:03:16,663 org.springframework.web.servlet.PageNotFound noHandlerFound - No mapping found for HTTP request with URI [/someUrlThatDoesNotExist] in DispatcherServlet with name 'dispatcher'
[DEBUG] 2014-01-24 10:03:16,665 org.springframework.web.servlet.DispatcherServlet processRequest - Successfully completed request

但是当我尝试使用 AOP 检查以下切入点时:

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpRequestMethodNotSupported(..)
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleNoSuchRequestHandlingMethod(..)

他们永远不会被抓住..

【问题讨论】:

    标签: java spring spring-mvc servlets


    【解决方案1】:

    我相信您需要配置您的 DispatcherServlet 以在未找到处理程序时抛出异常

    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    

    然后让您的 @ControllerAdvice 声明 @ExceptionHandler 以获取 NoHandlerFoundException 类型的异常。

    【讨论】:

    • 你有关于如何使用 AbstractAnnotationConfigDispatcherServletInitializer 做到这一点的例子吗?
    • @marcin 我现在无权访问代码,但该类应该有一个 configureDispatcherServlet() 方法,您可以重写它。
    • Sotirios Delimanolis 我认为 setThrowExceptionIfNoHandlerFound 仅在 spring 4.0 而非 3.2.6 dispatcherServlet 中可用
    • 仅对我来说看起来像 4.0。调度程序 servlet 上有一个受保护的方法,您可以覆盖它,称为 noHandlerFound。现在它只是将状态设置为 404,您可以改为抛出异常
    • @MarcinWasiluk 没有看到你的评论。如果不覆盖 3.2.6 中的一堆东西,你似乎无法做到。
    猜你喜欢
    • 2013-07-19
    • 2015-06-17
    • 2019-07-28
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多