java本服务调用本服务接口

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值