这个思路同样是通过在AbstractHandlerMethodMapping里面来实现getHandlerInternal()实现自己的方法来处理寻找正确的处理器,不懂得请看上一篇。

     protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
String lookupPath = this.getUrlPathHelper().getLookupPathForRequest(request); //同样从请求中获取url
if (this.logger.isDebugEnabled()) {
this.logger.debug("Looking up handler method for path " + lookupPath);
} HandlerMethod handlerMethod = this.lookupHandlerMethod(lookupPath, request);//查找handler
if (this.logger.isDebugEnabled()) {
if (handlerMethod != null) {
this.logger.debug("Returning handler method [" + handlerMethod + "]");
} else {
this.logger.debug("Did not find handler method for [" + lookupPath + "]");
}
} return handlerMethod != null ? handlerMethod.createWithResolvedBean() : null; //这块应该是为handler添加一些东西,参数等,
}
     protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
List<AbstractHandlerMethodMapping<T>.Match> matches = new ArrayList();
List<T> directPathMatches = (List)this.urlMap.get(lookupPath); //找到匹配url的匹配条件List
if (directPathMatches != null) {
this.addMatchingMappings(directPathMatches, matches, request); //添加匹配对象 Match实际上是匹配条件,和处理器的封装对象
} if (matches.isEmpty()) {
this.addMatchingMappings(this.handlerMethods.keySet(), matches, request);
} if (!matches.isEmpty()) {
Comparator<AbstractHandlerMethodMapping<T>.Match> comparator = new AbstractHandlerMethodMapping.MatchComparator(this.getMappingComparator(request));
Collections.sort(matches, comparator);
if (this.logger.isTraceEnabled()) {
this.logger.trace("Found " + matches.size() + " matching mapping(s) for [" + lookupPath + "] : " + matches);
}
       //如果多个匹配条件,进行排序,子类自己实现比较方法,然后取第一个
AbstractHandlerMethodMapping<T>.Match bestMatch = (AbstractHandlerMethodMapping.Match)matches.get();
if (matches.size() > ) { //如果存在两个复核条件的一样的处理器,则报错
AbstractHandlerMethodMapping<T>.Match secondBestMatch = (AbstractHandlerMethodMapping.Match)matches.get();
if (comparator.compare(bestMatch, secondBestMatch) == ) {
Method m1 = bestMatch.handlerMethod.getMethod();
Method m2 = secondBestMatch.handlerMethod.getMethod();
throw new IllegalStateException("Ambiguous handler methods mapped for HTTP path '" + request.getRequestURL() + "': {" + m1 + ", " + m2 + "}");
}
} this.handleMatch(bestMatch.mapping, lookupPath, request); //实际上缓存到request域中 //这块子类也复写了,应该是多缓存了些东西
return bestMatch.handlerMethod;
} else {
return this.handleNoMatch(this.handlerMethods.keySet(), lookupPath, request); //没找到子类已进行了复写
}
}
 RequestMappingInfoHandlerMapping 中 handleNoMatch 的实现
     protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> requestMappingInfos, String lookupPath, HttpServletRequest request) throws ServletException {
Set<String> allowedMethods = new HashSet();
Set<RequestMappingInfo> patternMatches = new HashSet();
Set<RequestMappingInfo> patternAndMethodMatches = new HashSet();
Iterator i$ = requestMappingInfos.iterator(); while(true) {
while(true) {
RequestMappingInfo info;
do {
if (!i$.hasNext()) {
if (patternMatches.isEmpty()) {
return null;
} if (patternAndMethodMatches.isEmpty() && !allowedMethods.isEmpty()) {
throw new HttpRequestMethodNotSupportedException(request.getMethod(), allowedMethods);
} Set consumableMediaTypes;
Set producibleMediaTypes;
if (patternAndMethodMatches.isEmpty()) {
consumableMediaTypes = this.getConsumableMediaTypes(request, patternMatches);
producibleMediaTypes = this.getProdicubleMediaTypes(request, patternMatches);
} else {
consumableMediaTypes = this.getConsumableMediaTypes(request, patternAndMethodMatches);
producibleMediaTypes = this.getProdicubleMediaTypes(request, patternAndMethodMatches);
} if (!consumableMediaTypes.isEmpty()) {
MediaType contentType = null;
if (StringUtils.hasLength(request.getContentType())) {
contentType = MediaType.parseMediaType(request.getContentType());
} throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList(consumableMediaTypes));
} if (!producibleMediaTypes.isEmpty()) {
throw new HttpMediaTypeNotAcceptableException(new ArrayList(producibleMediaTypes));
} return null;
} info = (RequestMappingInfo)i$.next();
} while(info.getPatternsCondition().getMatchingCondition(request) == null); patternMatches.add(info);
if (info.getMethodsCondition().getMatchingCondition(request) != null) {
patternAndMethodMatches.add(info);
} else {
Iterator i$ = info.getMethodsCondition().getMethods().iterator(); while(i$.hasNext()) {
RequestMethod method = (RequestMethod)i$.next();
allowedMethods.add(method.name());
}
}
}
}
}

实际上这一步是又匹配了一遍,没有在返回空, 有的话 就报异常

05-11 13:22