java本服務如何調用本服務接口
更新時間:2025年04月21日 08:47:40 作者:little_fairy66
這篇文章主要介紹了java本服務如何調用本服務接口問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
java本服務調用本服務Feign接口
- 1.獲取所有@RequestMaping注解修飾的方法
- 2.構建目標url的RequestMappingInfo對象
- 3.獲取目標url的Method對象
- 4.執(zhí)行
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的“參數(shù)”條件
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的名稱,入?yún)㈩愋?,返回?shù)據(jù),方法名稱等),源碼及注釋
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;
// 封裝方法參數(shù)的類實例,一個MethodParameter就是一個參數(shù)
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.執(zhí)行
注意被執(zhí)行的一定是可以進行實例化的類,不可以為接口或抽象類
method.invoke(method.getDeclaringClass()); // 若存在參數(shù),則使用以下方案,其中param為入?yún)? method.invoke(method.getDeclaringClass(), param);
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
ConditionalOnProperty注解的作用和使用方式
在SpringBoot項目開發(fā)中,@ConditionalOnProperty注解允許根據(jù)配置文件中的屬性值來控制配置類是否生效,該注解通過屬性name和havingValue來判斷配置是否注入,如果application.properties中的對應屬性值為空或不匹配havingValue設定值2024-09-09
源碼解析Spring 數(shù)據(jù)庫異常抽理知識點總結
在本篇文章里小編給大家分享了關于源碼解析Spring 數(shù)據(jù)庫異常抽理知識點內容,對此有需要的朋友們學習參考下。2019-05-05
java普通項目讀取不到resources目錄下資源文件的解決辦法
這篇文章主要給大家介紹了關于java普通項目讀取不到resources目錄下資源文件的解決辦法,Web項目中應該經(jīng)常有這樣的需求,在maven項目的resources目錄下放一些文件,比如一些配置文件,資源文件等,需要的朋友可以參考下2023-09-09
Spring Cloud Gateway實現(xiàn)灰度發(fā)布方案
灰度發(fā)布是在微服務中的表現(xiàn)為同一服務同時上線不同版本,讓一部分用戶使用新版本來驗證新特性,如果驗證沒有問題,則將所有用戶都遷移到新版本上,本文就來介紹一下如何實現(xiàn),感興趣的可以了解一下2023-12-12

