异常处理_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”; } |
- 出现异常错误
![]() |
- 出现异常交给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; } |
上一篇: 大数据培训课程之Hadoop编译源码jar包安装
下一篇: java培训课程之异常处理_SimpleMappingExceptionResolver