private Map<String /** url **/, Map.Entry<HandlerMethod, Set<RequestMethod>>> parseServiceBHandlerMethodMapping() {
if (servletContext == null) {
return new HashMap<>();
}
final Map<String /** url **/, Map.Entry<HandlerMethod, Set<RequestMethod>>> handlerMethodMapping = new HashMap<>();
WebApplicationContext webContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(webContext,
HandlerMapping.class, true, false);
if (allRequestMappings.size() == 0) {
return handlerMethodMapping;
}
for (HandlerMapping handlerMapping : allRequestMappings.values()) {
if (!(handlerMapping instanceof RequestMappingHandlerMapping)) {
continue;
}
RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerMapping;
Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
HandlerMethod handlerMethod = entry.getValue();
// 这里只处理ServiceB 包路径下的controller
if (!handlerMethod.getMethod().getDeclaringClass().getName().contains(".serviceB.")) {
continue;
}
// 这里只针对加上了@LocalCall注解的方法去解析,有些方法依赖http上下文,不能直接转为本地方法调用
if (handlerMethod.getMethodAnnotation(LocalCall.class) == null) {
continue;
}
RequestMappingInfo requestMappingInfo = entry.getKey();
if (CollectionUtils.isEmpty(requestMappingInfo.getDirectPaths())) {
continue;
}
Set<RequestMethod> requestMethodSet = requestMappingInfo.getMethodsCondition().getMethods();
if (CollectionUtils.isEmpty(requestMethodSet)) {
continue;
}
log.info("parseServiceBHandlerMethodMapping, urls={}, request_methods={}, method={}",
requestMappingInfo.getDirectPaths(),
requestMappingInfo.getMethodsCondition().getMethods(),
handlerMethod.getMethod());
for (String url : requestMappingInfo.getDirectPaths()) {
// URL_PREFIX实际是ServiceA的context-path
handlerMethodMapping.put(URL_PREFIX + url, new AbstractMap.SimpleImmutableEntry(handlerMethod, requestMethodSet));
}
}
}
return handlerMethodMapping;
}