詳解SpringMVC中的異常處理

1. SpringMVC默認三個異常處理類

  • ExceptionHandlerExceptionResolver:處理@ExceptionHandler註解
  • ResponseStatusExceptionResolver:處理@ResponseStatus註解
  • DefaultHandlerExceptionResolver:處理SpringMVC自帶的異常

如果以上3個異常解析器都無法處理,會上拋給tomcat,處理異常內部的默認工作流程:所有異常解析器依次嘗試解析,解析完成進行後續操作,解析失敗,下一個解析器繼續嘗試解析。

2. @ExceptionHandler註解異常

@ExceptionHandler標註在方法上

  • 方法上寫一個Exception用來接收發生的異常。
  • 要攜帶異常信息不能給參數位置寫Model,正確的做法是返回ModelAndView。
  • 如果有多個@ExceptionHandler都能處理這個異常,精確優先。
@ExceptionHandler(value = { ArithmeticException.class, NullPointerException.class }) 
// 告訴SpringMVC,這個方法專門處理這個類發送的所有異常
public ModelAndView handleException01(Exception exception) {
  System.out.println("handleException01..." + exception);
  ModelAndView view = new ModelAndView("myerror");
  view.addObject("ex", exception);  
  return view;
}

@ExceptionHandler統一異常處理

  • 將@ExceptionHandler放在一個單獨的類中,進行全局異常處理
  • 統一異常管理類需要通過@ControllerAdvice註解加入IoC容器中
  • 全局異常處理與本類異常處理同時存在,本類優先
@ControllerAdvice
public class MyException {
  // 處理空指針異常
  @ExceptionHandler(value = { NullPointerException.class })
  public ModelAndView handleException01(Exception exception) {
    System.out.println("全局的handleException01..." + exception);
    ModelAndView view = new ModelAndView("myerror");
    view.addObject("ex", exception);
    return view;
  }

  // 處理算數異常
  @ExceptionHandler(value = { ArithmeticException.class })
  public ModelAndView handleException02(Exception exception) {
    System.out.println("全局的handleException02..." + exception);
    ModelAndView view = new ModelAndView("myerror");
    view.addObject("ex", exception);
    return view;
  }
}

3. @ResponseStatus註解異常

@ResponseStatus註解標註在自定義異常上,用於設置自定義異常信息

@ResponseStatus(reason = "用戶被拒絕登錄", value = HttpStatus.NOT_ACCEPTABLE)
public class UsernameNotFoundException extends RuntimeException {
  private static final long serialVersionUID = 1L;
}

4. DefaultHandlerExceptionResolver默認異常

DefaultHandlerExceptionResolver包含以下默認異常

源碼:
public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionResolver {
  ...
  @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 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);
      }
    }
    catch (Exception handlerException) {
      if (logger.isWarnEnabled()) {
        logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException);
      }
    }
    return null;
  }
  ...
}

如下HttpRequestMethodNotSupportedException請求方式不對。使用GET對POST方法進行訪問

@RequestMapping(value = "/handle03", method = RequestMethod.POST)
public String postMethod() {
  return "success";
}

5. 沒有找到對應異常處理類

若沒有對應異常處理類,會進入對應服務器錯誤頁面

以上就是詳解SpringMVC中的異常處理的詳細內容,更多關於SpringMVC 異常處理的資料請關註WalkonNet其它相關文章!

推薦閱讀: