Spring3.2Servlet3.0Java配置(无web.xml)如何创建自定义404处理程序
在不使用Spring3.2Servlet3.0Java配置(无web.xml)如何创建自定义404处理程序,java,spring,spring-mvc,servlets,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;
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);
}
}
然而
@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。。。不走运
当我调用/someurl时,它不是文本列表:
[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);
然后让您的
@ControllerAdvice
为类型为NoHandlerFoundException
的异常声明一个@ExceptionHandler
异常处理程序。您有关于如何使用AbstractAnnotationConfigDispatchers ServletInitializer执行此操作的示例吗?@marcin我现在无权访问代码,但是该类应该有一个configureDispatcherServlet()方法,您可以重写它。Sotirios Delimanolis我认为SetThroweExceptionIfNoHandlerFound只在spring 4.0中可用,而不是3.2.6 Dispatcherservlet仅在我看来像4.0。DispatcherServlet上有一个受保护的方法,您可以重写它,称为noHandlerFound。现在它只是将状态设置为404,您可以抛出一个异常instead@MarcinWasiluk我没有看到你的评论。如果不覆盖3.2.6中的一系列内容,您似乎无法做到这一点。
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);