java - Spring 3.2 servlet 3.0 Java Config(无 web.xml)如何创建自定义 404 处理程序

标签 java spring spring-mvc servlets

在不使用 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(..)

他们永远不会被捕获..

最佳答案

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

dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

然后让您的 @ControllerAdviceNoHandlerFoundException 类型的异常声明一个 @ExceptionHandler

关于java - Spring 3.2 servlet 3.0 Java Config(无 web.xml)如何创建自定义 404 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21330267/

相关文章:

日期 JSON 序列化程序将列表转换为字符串的 java.lang.reflect.InitationTargetException

java - 在 Spring 中注册自定义属性编辑器

spring - 在 Spring 中结合 GET 和 POST 请求方法

java - 从网络应用程序停止/重启计算机的功能

java - JSP 模板教程。如何使用 jar 文件

java SocketException : Connection reset

javascript - $resource.save 使用 "Resource"而不是简单的 ID 进行响应

java - 正则表达式不匹配尾随文本

java - Swift 3.0 中的 FileHandle 和 java 中的 FileInputStream 的区别

java - Spring MVC : Could not resolve placeholder in string value