我想在Spring MVC App中处理未映射的urls / 404错误我找到了一个示例Here SO Answer,我基于Java的配置并尝试了这种方式
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "com.app.controller" })
public class ServletConfigurer extends WebMvcConfigurerAdapter {
private Properties errorResolverProperties;
private Properties errorProperties;
/// Here I'm configuring <beans as mentioned in SO Answer
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
errorResolverProperties = new Properties();
errorProperties = new Properties();
errorProperties.put("/**", pageNotFoundController());
errorResolverProperties.put("mappings", errorProperties);
return simpleUrlHandlerMapping;
}
// this is my Controller
@Bean
public PageNotFoundController pageNotFoundController(){
return new PageNotFoundController();
}
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
tilesConfigurer.setDefinitions("/WEB-INF/tiles_xml/tiles.xml");
return tilesConfigurer;
}
}
我的控制器是
@Controller
public class PageNotFoundController {
@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleINFException(PageNotFoundException ex) {
return "error";
}
}
最后
public class PageNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
public PageNotFoundException(String message) {
super(message);
}
}
但它总是显示与Apache相同的错误页面而不是自定义/我的错误页面。
public class AppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext rootContext = getWebApplicationContext();
servletContext.addListener(new ContextLoaderListener(rootContext));
servletContext.setInitParameter("defaultHtmlEscape", "true");
// add the dispatcher servlet and map it to /
DispatcherServlet dispatcherServlet = new DispatcherServlet(rootContext);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
"springDispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
private AnnotationConfigWebApplicationContext getWebApplicationContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.app.config");
return context;
}
}
就是它..
答案 0 :(得分:2)
如果要全局捕获它,则需要一个ControllerAdvice:
@ControllerAdvice
public class ExceptionHandlerController {
public static final String DEFAULT_ERROR_VIEW = "error";
public static final String STATUS_CODE = "404";
public static final String TYPE = "Custom Type";
@ExceptionHandler(value = {NoHandlerFoundException.class})
public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) {
ModelAndView mav = new ModelAndView(DEFAULT_ERROR_VIEW);
mav.addObject("timestamp", new Date());
mav.addObject("status", STATUS_CODE);
mav.addObject("type", TYPE);
mav.addObject("message", String.format("The requested url is: %s", request.getRequestURL()));
return mav;
}
}
现在你需要激活,在404的情况下抛出异常:
@Autowired
public void configureDispatcher(DispatcherServlet dispatcherServlet){
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
}
将其插入任何@Configuration带注释的类中。
那就是它!
<强>更新强>
将班级更改为
@Configuration
public class AppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext rootContext = getWebApplicationContext();
servletContext.addListener(new ContextLoaderListener(rootContext));
servletContext.setInitParameter("defaultHtmlEscape", "true");
}
private AnnotationConfigWebApplicationContext getWebApplicationContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.app.config");
return context;
}
@Autowired
public void configureDispatcher(DispatcherServlet dispatcherServlet){
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
}
}