Spring MVC 使用 HandlerExceptionResolver 进行异常处理
我目前正在尝试在 Spring MVC 项目中使用 HandlerExceptionResolver 进行异常处理。
我想通过 resolveException
以及 404 的方式处理正常异常
handleNoSuchRequestHandlingMethod
。
根据请求类型 JSON 或 text/html,应适当返回异常响应。
resolveException
现在可以工作了。
但是handleNoSuchRequestHandlingMethod
让我很头疼。它从来没有被调用过!
根据文档,应该在 404 错误时调用该方法
我是什么做错了......
这就是我到目前为止所拥有的。
public class JsonExceptionResolver implements HandlerExceptionResolver {
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView resolveException(HttpServletRequest request,
if (exception instanceof NoSuchRequestHandlingMethodException) {
return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) exception, request, response, handler);
}
...
}
public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex,
HttpServletRequest request,
HttpServletResponse response,
Object handler){
logger.info("Handle my exception!!!");
ModelAndView mav = new ModelAndView();
boolean isJSON = request.getHeader("Accept").equals("application/json");
if(isJSON){
...
}else{
..
}
return mav;
}
}
用DefaultHandlerExceptionResolver编辑:
public class MyExceptionResolver extends DefaultHandlerExceptionResolver {
protected final Log logger = LogFactory.getLog(getClass());
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {
logger.warn("An Exception has occured in the application", exception);
logger.info("exception thrown " + exception.getMessage() );
if (exception instanceof NoSuchRequestHandlingMethodException) {
return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) exception, request, response, handler);
}
...
return mav;
}
public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex,
HttpServletRequest request,
HttpServletResponse response,
Object handler){
logger.info("Handle my exception!!!");
ModelAndView mav = new ModelAndView();
boolean isJSON = request.getHeader("Accept").equals("application/json");
if(isJSON){
...
}else{
...
}
return mav;
}
}
上面的代码仍然没有效果。
还有其他想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 Spring 的 Juergen Hoeller 的说法,使用
HandlerExceptionResolver
是不可能的,因为它仅适用于子映射,例如您有一个映射到
/account/**
的控制器,并且从 acount 访问一个不存在映射的方法,例如/acount/notExists
,而不是它应该工作的方法。我将针对此功能开具 JIRA 改进票证
编辑:
关于此问题的 JIRA 票证
https://jira.springsource.org/browse/SPR-8837?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=72648#comment-72648
According to Juergen Hoeller from Spring, it isn't possible with the
HandlerExceptionResolver
because it only works for sub-mapping e.g.you have a controller mapped to
/account/**
and accesss a method from acount where no mapping exists like/acount/notExists
than it should work.I will open a JIRA improvement ticket for this functionality
EDIT:
JIRA ticket about this issue
https://jira.springsource.org/browse/SPR-8837?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=72648#comment-72648
handleNoSuchRequestHandlingMethod
不是HandlerExceptionResolver
接口的一部分,因此仅声明该名称的方法不会执行任何操作。它是特定于DefaultHandlerExceptionResolver
的受保护方法,并从其resolveException
方法(该是接口的一部分)调用:
重现相同的功能,您可以子类
DefaultHandlerExceptionResolver
并重写您需要的方法,或者您需要在resolveException
方法中添加一个案例来处理NoSuchRequestHandlingMethodException
。handleNoSuchRequestHandlingMethod
isn't part of theHandlerExceptionResolver
interface, so just declaring a method of that name will do nothing. It's a protected method specific toDefaultHandlerExceptionResolver
, and is called from itsresolveException
method (which is part of the interface):To reproduce the same functionality, you can either subclass
DefaultHandlerExceptionResolver
and override the methods you need to, or you need to add a case in yourresolveException
method that handlesNoSuchRequestHandlingMethodException
.