實現思路
請求路徑如:/person/list/101。使用正則將id爲key,101爲value填充到map中放到request域中。爲目標方法形參設置參數時拿出request域中的值然後賦值。
實現原理
@PathVariable註解對應的解析器是PathVariableMethodArgumentResolver
根據HandlerMapping映射處理器獲得HandlerExecutionChain
mappedHandler = getHandler(processedRequest);
獲得請求的目標方法的包裝類HandlerMethod
Object handler = getHandlerInternal(request);
查找與請求路徑正則匹配的目標方法
HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
List<Match> matches = new ArrayList<Match>();
// 獲得匹配的映射Match(RequestMappingInfo, HandlerMethod)
addMatchingMappings(this.mappingRegistry.getMappings().keySet(), matches, request);
// 處理匹配成功的映射
handleMatch(bestMatch.mapping, lookupPath, request);