Spring MVC如何使用Java配置和无Web.xml处理404页面未找到异常
问题内容:
我想在我的Spring MVC Web应用程序中处理404页面未找到异常,我正在使用SPRING 4.2.5.RELEASE
,我已经阅读了有关此主题的几个问题,但是类似的问题正在使用不同的spring java配置。
我有一个具有所有异常的Global Exception Handler Controller类,该类工作正常,但我无法处理404页面未找到异常。
这是我遵循教程的方法
1)我创建了一个名为ResourceNotFoundException
from
的类,RuntimeException
并将此注释放在类定义上@ResponseStatus(HttpStatus.NOT_FOUND)
像这样:
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
}
2)我在异常的控制器类中创建了此方法
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException() {
return "notFoundJSPPage";
}
但是,当我放置一个不存在的URL时,仍然出现此错误“找不到具有URI的HTTP请求的映射”
我读过的问题说我需要为Dispatcher启用一个选项,但是由于我的配置与其他问题不同,我没有,Web.xml
所以无法应用。
这是我的Config.java
@EnableWebMvc
@Configuration
@ComponentScan({"config", "controllers"})
public class ConfigMVC extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
}
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
这是我的WebInitializer
public class WebInicializar implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ConfigMVC.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
这是我的全局异常处理程序控制器
@ControllerAdvice
public class GlobalExceptionHandlerController {
@ExceptionHandler(value = NullPointerException.class)
public String handleNullPointerException(Exception e) {
System.out.println("A null pointer exception ocurred " + e);
return "nullpointerExceptionPage";
}
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = Exception.class)
public String handleAllException(Exception e) {
System.out.println("A unknow Exception Ocurred: " + e);
return "unknowExceptionPage";
}
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException() {
return "notFoundJSPPage";
}
}
我创建的扩展了Runtime Exception的类
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException{
}
问题答案:
我通过在我的onStartup
方法中将该行放入WebApplicationInitializer.class
这是我添加的行 servlet.setInitParameter("throwExceptionIfNoHandlerFound", "true");
这就是我添加的新行的完整方法的外观
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ConfigMVC.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
servlet.setInitParameter("throwExceptionIfNoHandlerFound", "true");
}
然后我在我的控制器中创建了这个控制器方法 GlobalExceptionHandlerController.class
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handle(NoHandlerFoundException ex) {
return "my404Page";
}
这解决了我的问题,我删除了handleResourceNotFoundException
控制器方法,GlobalExceptionHandlerController.class
因为这是不必要的,而且我还删除了ResourceNotFoundException.class
我创建的异常类