我有简单的Spring MVC应用程序,我想在我的Advice Controller类中找到404未找到的异常
配置:
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfig.class};
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class, SecurityConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addListener(new SessionListener());
FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter());
encodingFilter.setInitParameter("encoding", "UTF-8");
encodingFilter.setInitParameter("forceEncoding", "true");
encodingFilter.addMappingForUrlPatterns(null, true, "/*");
}
}
控制器:
@Controller
@RequestMapping("/")
public class HomeController {
@RequestMapping(value = "/error/", method = RequestMethod.GET)
public String error(){return "error";}
}
ControllerAdvice:
@ControllerAdvice
public class AdviceController {
@ExceptionHandler(MyOwnException.class)
@ResponseStatus(value= HttpStatus.BAD_REQUEST)
public String checkoutException(CheckoutException e, HttpServletRequest httpServletRequest) {
return "error";
}
}
当我手动抛出MyOwnException但我无法捕获NoHandlerFound异常时,我可以捕获自己的异常。当没有控制器方法来处理请求时,我需要发送404错误代码和相应的error.jsp页面
答案 0 :(得分:4)
如果您的网络应用程序使用web.xml
,则非常简单 - 只需添加以下内容(假设使用 InternalResourceViewResolver ,前缀指向 WEB-INF 视图文件夹和后缀 .jsp )。您也可以拥有其他错误代码的多个错误页面元素。
<error-page>
<error-code>404</error-code>
<location>/error</location>
</error-page>
如果您没有使用web.xml
,则会更复杂,您必须定义并注册自己的ExceptionResolver
。有关如何执行此操作的详细信息,请查看此spring.io blog article。
(评论后编辑)
如果你想抓住NoHandlerFound
例外,首先要告诉Spring通过setting a flag in the DispatcherServlet directly抛出它。为此,在AppInitializer
类中添加DispatcherServlet定义,以添加标志:
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addListener(new SessionListener());
//BEGIN OF NEW CODE
WebApplicationContext context = getContext();
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
//we did all this to set the below flag
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet",dispatcherServlet );
//END OF NEW CODE
FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter());
encodingFilter.setInitParameter("encoding", "UTF-8");
encodingFilter.setInitParameter("forceEncoding", "true");
encodingFilter.addMappingForUrlPatterns(null, true, "/*");
}
然后,您可以直接在AdviceController
:
@ControllerAdvice
public class AdviceController {
//..
@ExceptionHandler(NoHandlerFoundException.class)
public String dealWithNoHandlerFoundException(CheckoutException e, HttpServletRequest httpServletRequest) {
return "error";
}
}