微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring MVC入门十:异常处理

异常处理

  • 简介
  • @H_502_5@
    SpringMVC提供了一个处理控制器方法执行过程中所出现的异常的接口:HandlerExceptionResolver
    HandlerExceptionResolver接口的实现类有:DefaultHandlerExceptionResolver和
    SimpleMappingExceptionResolver
    SpringMVC提供了自定义的异常处理器SimpleMappingExceptionResolver
    
    • 双击shift,搜索DefaultHandlerExceptionResolver

    • ctrl + h 选中AbstractHandlerExceptionResolver

    • 查看DefaultHandlerExceptionResolver源码,当出现异常时,返回1个新的ModelAndView替换我们自己的ModelAndView

    • @H_502_5@
              @Override
      	@Nullable
      	protected ModelAndView doResolveException(
      			HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
      
      		try {
      			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 MissingPathVariableException) {
      				return handleMissingPathVariable(
      						(MissingPathVariableException) 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);
      			}
      			else if (ex instanceof AsyncRequestTimeoutException) {
      				return handleAsyncRequestTimeoutException(
      						(AsyncRequestTimeoutException) ex, request, response, handler);
      			}
      		}
      		catch (Exception handlerEx) {
      			if (logger.isWarnEnabled()) {
      				logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", handlerEx);
      			}
      		}
      		return null;
      	}
      

      版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐