java本服务调用本服务Feign接口
1.获取所有@RequestMaping注解修饰的方法
2.构建目标url的RequestMappingInfo对象
3.获取目标url的Method对象
4.执行
1.获取所有@RequestMaping注解修饰的方法
Map<RequestMappingInfo, HandlerMethod> handlerMethods =
requestMappingHandlerMapping.getHandlerMethods();
获取结果类型为Map<RequestMappingInfo, HandlerMethod> 其中RequestMappingInfo为请求映射信息(包含请求类型,url)
public final class RequestMappingInfo implements RequestCondition<RequestMappingInfo> {
// 这个映射对象的名字
@Nullable
private final String name;
// 此RequestMappingInfo对象的URL模式
@Nullable
private final PathPatternsRequestCondition pathPatternsCondition;
// 此RequestMappingInfo的HTTP请求方法;
@Nullable
private final PatternsRequestCondition patternsCondition;
private final RequestMethodsRequestCondition methodsCondition;
// 此RequestMappingInfo的“参数”条件
private final ParamsRequestCondition paramsCondition;
// 此RequestMappingInfo的“headers”条件;
private final HeadersRequestCondition headersCondition;
private final ConsumesRequestCondition consumesCondition;
private final ProducesRequestCondition producesCondition;
private final RequestConditionHolder customConditionHolder;
private final int hashCode;
private final BuilderConfiguration options;
}
HandlerMethod为该bean中对应方法的信息(包括bean的名称,入参类型,返回数据,方法名称等),源码及注释
public class HandlerMethod {
// bean的名称,可通俗理解为类名
private final Object bean;
@Nullable
private final BeanFactory beanFactory;
@Nullable
private final MessageSource messageSource;
// 方法所属类
private final Class<?> beanType;
// 对应方法
private final Method method;
// 被桥接的方法,如果method是原生的,这个属性的值就是method
private final Method bridgedMethod;
// 封装方法参数的类实例,一个MethodParameter就是一个参数
private final MethodParameter[] parameters;
@Nullable
private HttpStatus responseStatus;
@Nullable
private String responseStatusReason;
@Nullable
private HandlerMethod resolvedFromHandlerMethod;
@Nullable
private volatile List<Annotation[][]> interfaceParameterAnnotations;
private final String description;
}
2.构建目标url的RequestMappingInfo对象
RequestMethod requestMethod = RequestMethod.GET ;
RequestMappingInfo requestMappingInfo =
RequestMappingInfo.paths("/export/getInfo").methods(requestMethod).build();
3.获取目标url的Method对象
Method method = handlerMethods.get(requestMappingInfo).getMethod();
4.执行
注意被执行的一定是可以进行实例化的类,不可以为接口或抽象类
method.invoke(method.getDeclaringClass());
// 若存在参数,则使用以下方案,其中param为入参
method.invoke(method.getDeclaringClass(), param);