Java培训之异常处理_DefaultHandlerExceptionResolver

?异常处理_DefaultHandlerExceptionResolver

1)对一些特殊的异常进行处理,比如:

  • NoSuchRequestHandlingMethodException、
  • HttpRequestMethodNotSupportedException
  • HttpMediaTypeNotSupportedException、
  • HttpMediaTypeNotAcceptableException等。

2)javadoc

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver

Default implementation of the HandlerExceptionResolver interface that resolves standard Spring exceptions and translates them to corresponding HTTP status codes.

This exception resolver is enabled by default in the org.springframework.web.servlet.DispatcherServlet.

? ?1 实验代码

  • 增加页面链接:GET请求

<a href=”testDefaultHandlerExceptionResolver”>testDefaultHandlerExceptionResolver</a>

增加处理器方法

//@RequestMapping(value=”/testDefaultHandlerExceptionResolver”)

@RequestMapping(value=”/testDefaultHandlerExceptionResolver”,method=RequestMethod.POST) ?//不支持GET请求

public String testDefaultHandlerExceptionResolver(){

System.out.println(“testDefaultHandlerExceptionResolver…”);

return “success”;

}

  • 出现异常错误
Java培训
  • 出现异常交给DefaultHandlerExceptionResolver处理

@Override

protected ModelAndView doResolveException(

HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

 

try {

if (ex instanceof NoSuchRequestHandlingMethodException) {

return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, request, response,

handler);

}

else if (ex instanceof HttpRequestMethodNotSupportedException) {

return handleHttpRequestMethodNotSupported((HttpRequestMethodNotSupportedException) ex, request,

response, handler);

}

else if (ex instanceof HttpMediaTypeNotSupportedException) {

return handleHttpMediaTypeNotSupported((HttpMediaTypeNotSupportedException) ex, request, response,

handler);

}

else if (ex instanceof HttpMediaTypeNotAcceptableException) {

return handleHttpMediaTypeNotAcceptable((HttpMediaTypeNotAcceptableException) ex, request, response,

handler);

}

else if (ex instanceof MissingServletRequestParameterException) {

return handleMissingServletRequestParameter((MissingServletRequestParameterException) ex, request,

response, handler);

}

else if (ex instanceof ServletRequestBindingException) {

return handleServletRequestBindingException((ServletRequestBindingException) ex, request, response,

handler);

}

else if (ex instanceof ConversionNotSupportedException) {

return handleConversionNotSupported((ConversionNotSupportedException) ex, request, response, handler);

}

else if (ex instanceof TypeMismatchException) {

return handleTypeMismatch((TypeMismatchException) ex, request, response, handler);

}

else if (ex instanceof HttpMessageNotReadableException) {

return handleHttpMessageNotReadable((HttpMessageNotReadableException) ex, request, response, handler);

}

else if (ex instanceof HttpMessageNotWritableException) {

return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, request, response, handler);

}

else if (ex instanceof MethodArgumentNotValidException) {

return handleMethodArgumentNotValidException((MethodArgumentNotValidException) ex, request, response, handler);

}

else if (ex instanceof MissingServletRequestPartException) {

return handleMissingServletRequestPartException((MissingServletRequestPartException) ex, request, response, handler);

}

else if (ex instanceof BindException) {

return handleBindException((BindException) ex, request, response, handler);

}

else if (ex instanceof NoHandlerFoundException) {

return handleNoHandlerFoundException((NoHandlerFoundException) ex, request, response, handler);

}

}

catch (Exception handlerException) {

logger.warn(“Handling of [” + ex.getClass().getName() + “] resulted in Exception”, handlerException);

}

return null;

}


上一篇:
下一篇:
关于我们
名师团队
新闻动态
合作企业
资源下载
视频下载
资料下载
工具下载
加入我们
招聘岗位
岗位介绍
招贤纳师
联系我们
电话:010-56253825
邮箱:[email protected]
地址:北京市昌平区宏福科技园综?#19979;?层(北京校区)

 深圳市宝安区西部硅谷大厦B座C区一层(深圳校区)

上海市松江区谷阳北路166号大江商厦6层(上海校区)

轩辕传奇通天塔
3d开机号走势图带连线专业版 组六全包稳赚 pt电子游戏输了很多 黑龙江36选7彩票 北京快中彩基本走势图 重庆时时到底怎样 超级大乐透下载最新版 经典欢乐斗地主手机版 浙江省快乐12的走势图 澳洲幸运5基本走势图