xml方式配置异常处理器


xml方式配置异常处理器

异常处理器:

当控制器方法出现指定异常(非指定异常)的时候,返回一个modelandview对象给dispatcherServlet

spring默认的异常处理器:

defaultExceptionResolver,已注册到ioc,出现异常时dispatcherServlet默认调用该异常处理器,处理包含的所有异常类型

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;
}

自定义的异常处理器:

SampleMappingExceptionResolver,需要注册到ioc中,控制器方法出现指定的异常时,由dispatcherServlet调用


 
     
         
     

 

 
 


 
              要指定要处理的异常
 

             
          以ex(自定义)为键,以错误为值,将该键值对共享到Request域对象中