日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解SpringBoot接口異常處理機制及源碼分析

環(huán)境:Springboot3.0.5

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比榆社網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式榆社網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋榆社地區(qū)。費用合理售后完善,十載實體公司更值得信賴。

概述

如果在請求映射期間發(fā)生異?;驈恼埱筇幚沓绦?例如@Controller)拋出異常,DispatcherServlet將委托給HandlerExceptionResolver。

下表列出了可用的HandlerExceptionResolver實現(xiàn)。

HandlerExceptionResolver 實現(xiàn)類:

HandlerExceptionResolver

描述

SimpleMappingExceptionResolver

異常類名和錯誤視圖名之間的映射。用于在瀏覽器應(yīng)用程序中渲染錯誤頁面。

DefaultHandlerExceptionResolver

解析Spring MVC引發(fā)的異常,并將其映射為HTTP狀態(tài)碼。

ResponseStatusExceptionResolver

使用@ResponseStatus注解解析異常,并根據(jù)注解中的值將異常映射為HTTP狀態(tài)碼。

ExceptionHandlerExceptionResolver

通過在@Controller或@ControllerAdvice類中調(diào)用由@ExceptionHandler注釋的方法來解決異常。

我們可以聲明多個HandlerExceptionResolver

HandlerExceptionResolver的約定規(guī)定它可以返回:

  • 指向錯誤視圖的ModelAndView。
  • 如果異常是在解析器中處理的,則返回空的ModelAndView。
  • 如果異常仍然未解決,則為null,以便后續(xù)的解析器嘗試,如果異常在最后仍然存在,則允許它向上冒泡到Servlet容器。

Controller接口調(diào)用原理

SpringMVC請求入口通過DispatcherServlet執(zhí)行大致核心流程如下:

  1. 首先通過HandlerMapping確定目標(biāo)Handler對象(如果接口是Controller那么這里會是 HandlerMethod)
  2. 通過上一步Handler對象,確定執(zhí)行真正調(diào)用的HandlerAdapter

這里以Controller接口為例,HandlerAdapter對象為RequestMappingHandlerAdapter。

DispatcherServlet

public class DispatcherServlet extends FrameworkServlet {
  protected void doDispatch(...) throws Exception {
    HandlerExecutionChain mappedHandler = null;
    try {
      Exception dispatchException = null;
      // 根據(jù)請求確定Handler對象(遍歷所有的HandlerMapping)
      mappedHandler = getHandler(processedRequest);
      // 根據(jù)上一步確定的Handler對象,確定HandlerAdapter對象
      HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
      // 真正執(zhí)行目標(biāo)方法的調(diào)用
      mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
    } catch (Exception ex) {
      dispatchException = ex;
    } catch (Throwable err) {
      dispatchException = new ServletException("Handler dispatch failed: " + err, err);
    }
    processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
  }
}

RequestMappingHandlerAdapter

public class RequestMappingHandlerAdapter {
  protected ModelAndView handleInternal(...) throws Exception {
    ModelAndView mav;
    mav = invokeHandlerMethod(request, response, handlerMethod);
  }
  protected ModelAndView invokeHandlerMethod(...) throws Exception {
    // ...
    ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
    // ... 對ServletInvocableHandlerMethod進行配置
    invocableMethod.invokeAndHandle(webRequest, mavContainer);
    return getModelAndView(mavContainer, modelFactory, webRequest);
  }
}

ServletInvocableHandlerMethod執(zhí)行參數(shù)解析目標(biāo)Controller方法調(diào)用及返回值的處理。

public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
  public void invokeAndHandle(...) throws Exception {
    // 該方法中會進行請求參數(shù)的解析及目標(biāo)方法的調(diào)用
    Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);
    // ...
    try {
      // 處理返回值
      this.returnValueHandlers.handleReturnValue(returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
    } catch (Exception ex) {
      throw ex;
    }
  }
}

通過上面的源碼分析,在調(diào)用過程中如果發(fā)生了異常會將異常直接拋出,在DispatcherServlet中會進行異常的處理。

異常解析原理分析

接著上面的源碼分析,當(dāng)發(fā)生異常后最終會在DispatcherServlet#processDispatchResult方法中進行處理。

public class DispatcherServlet extends FrameworkServlet {
  /*
    * 默認情況下有如下2個異常解析器
    * 1. DefaultErrorAttributes
    * 2. ExceptionHandlerExceptionResolver
    */
  private List handlerExceptionResolvers;
  private void processDispatchResult(...) {
    if (exception != null) {
      Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
      // 處理異常
      mv = processHandlerException(request, response, handler, exception);
    }
  }
  protected ModelAndView processHandlerException(...) throws Exception {
    ModelAndView exMv = null;
    if (this.handlerExceptionResolvers != null) {
      // 遍歷所有的異常解析器
      for (HandlerExceptionResolver resolver : this.handlerExceptionResolvers) {
        // 解析異常,核心的解析器是ExceptionHandlerExceptionResolver
        exMv = resolver.resolveException(request, response, handler, ex);
      }
    }
    // ...
  }
}

ExceptionHandlerExceptionResolver類繼承自AbstractHandlerMethodExceptionResolver該類又繼承自AbstractHandlerExceptionResolver。

// 調(diào)用父類(AbstractHandlerExceptionResolver)方法
public abstract class AbstractHandlerExceptionResolver implements HandlerExceptionResolver, Ordered {
  public ModelAndView resolveException(...) {
    // doResolveException該方法在子類AbstractHandlerMethodExceptionResolver中重寫
    ModelAndView result = doResolveException(request, response, handler, ex);
  }
}

AbstractHandlerMethodExceptionResolver

public abstract class AbstractHandlerMethodExceptionResolver extends AbstractHandlerExceptionResolver {
  protected final ModelAndView doResolveException(...) {
    HandlerMethod handlerMethod = (handler instanceof HandlerMethod hm ? hm : null);
    return doResolveHandlerMethodException(request, response, handlerMethod, ex);
  }
}

ExceptionHandlerExceptionResolver

public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExceptionResolver implements ApplicationContextAware, InitializingBean {
  protected ModelAndView doResolveHandlerMethodException(...) {
    // 該方法中會先從當(dāng)前的Controller中查找是否有@ExceptionHandler注解的方法(如果匹配)
    // 如果沒有再從全局的異常處理類句柄中查找
    ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
    if (exceptionHandlerMethod == null) {
      return null;
    }
    // 執(zhí)行異常處理方法的調(diào)用
    exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, arguments);
  }
  protected ServletInvocableHandlerMethod getExceptionHandlerMethod(...) {
    Class handlerType = null;
    if (handlerMethod != null) {  
      handlerType = handlerMethod.getBeanType();
      // 緩存并設(shè)置當(dāng)前執(zhí)行Class對應(yīng)的ExceptionHandlerMethodResolver
      // ExceptionHandlerMethodResolver構(gòu)造函數(shù)中會解析當(dāng)前類中的所有方法是否有@ExceptionHandler注解
      ExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.computeIfAbsent(handlerType, ExceptionHandlerMethodResolver::new);
      // 解析是否匹配當(dāng)前發(fā)生的異常
      Method method = resolver.resolveMethod(exception);
      if (method != null) {
        return new ServletInvocableHandlerMethod(handlerMethod.getBean(), method, this.applicationContext);
      }
    }
    // 如果上面的執(zhí)行的Class中沒有找到對應(yīng)處理器,那么就從全局的異常處理中進行查找匹配
    // 這里的exceptionHandlerAdviceCache集合在類初始化執(zhí)行時已經(jīng)處理完成
    for (Map.Entry entry : this.exceptionHandlerAdviceCache.entrySet()) {
      ControllerAdviceBean advice = entry.getKey();
      if (advice.isApplicableToBeanType(handlerType)) {
        ExceptionHandlerMethodResolver resolver = entry.getValue();
        Method method = resolver.resolveMethod(exception);
        if (method != null) {
          return new ServletInvocableHandlerMethod(advice.resolveBean(), method, this.applicationContext);
        }
      }
    }
    return null;
  }
}

通過上面的源碼分析你應(yīng)該知道了關(guān)于SpringMVC中異常處理的原理。

當(dāng)上面的異常處理機制都沒法處理,那么將會調(diào)用默認的/error接口。

public class ErrorMvcAutoConfiguration {
  @Bean
  @ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
  public BasicErrorController basicErrorController(ErrorAttributes errorAttributes, ObjectProvider errorViewResolvers) {
    return new BasicErrorController(errorAttributes, this.serverProperties.getError(), errorViewResolvers.orderedStream().toList());
  }  
}

BasicErrorController

@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
}

上面的錯誤接口/error在容器啟動時會自動注冊到內(nèi)嵌的容器中,如:Tomcat。


當(dāng)前名稱:詳解SpringBoot接口異常處理機制及源碼分析
本文URL:http://www.5511xx.com/article/dphddjg.html