java本服務(wù)如何調(diào)用本服務(wù)接口
java本服務(wù)調(diào)用本服務(wù)Feign接口
- 1.獲取所有@RequestMaping注解修飾的方法
- 2.構(gòu)建目標(biāo)url的RequestMappingInfo對(duì)象
- 3.獲取目標(biāo)url的Method對(duì)象
- 4.執(zhí)行
1.獲取所有@RequestMaping注解修飾的方法
Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
獲取結(jié)果類型為Map<RequestMappingInfo, HandlerMethod> 其中RequestMappingInfo為請(qǐng)求映射信息(包含請(qǐng)求類型,url)
public final class RequestMappingInfo implements RequestCondition<RequestMappingInfo> { // 這個(gè)映射對(duì)象的名字 @Nullable private final String name; // 此RequestMappingInfo對(duì)象的URL模式 @Nullable private final PathPatternsRequestCondition pathPatternsCondition; // 此RequestMappingInfo的HTTP請(qǐng)求方法; @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中對(duì)應(yīng)方法的信息(包括bean的名稱,入?yún)㈩愋?,返回?cái)?shù)據(jù),方法名稱等),源碼及注釋
public class HandlerMethod { // bean的名稱,可通俗理解為類名 private final Object bean; @Nullable private final BeanFactory beanFactory; @Nullable private final MessageSource messageSource; // 方法所屬類 private final Class<?> beanType; // 對(duì)應(yīng)方法 private final Method method; // 被橋接的方法,如果method是原生的,這個(gè)屬性的值就是method private final Method bridgedMethod; // 封裝方法參數(shù)的類實(shí)例,一個(gè)MethodParameter就是一個(gè)參數(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.構(gòu)建目標(biāo)url的RequestMappingInfo對(duì)象
RequestMethod requestMethod = RequestMethod.GET ; RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("/export/getInfo").methods(requestMethod).build();
3.獲取目標(biāo)url的Method對(duì)象
Method method = handlerMethods.get(requestMappingInfo).getMethod();
4.執(zhí)行
注意被執(zhí)行的一定是可以進(jìn)行實(shí)例化的類,不可以為接口或抽象類
method.invoke(method.getDeclaringClass()); // 若存在參數(shù),則使用以下方案,其中param為入?yún)? method.invoke(method.getDeclaringClass(), param);
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java提取json中某個(gè)數(shù)組的所有值方法
下面小編就為大家分享一篇java提取json中某個(gè)數(shù)組的所有值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03ConditionalOnProperty注解的作用和使用方式
在SpringBoot項(xiàng)目開(kāi)發(fā)中,@ConditionalOnProperty注解允許根據(jù)配置文件中的屬性值來(lái)控制配置類是否生效,該注解通過(guò)屬性name和havingValue來(lái)判斷配置是否注入,如果application.properties中的對(duì)應(yīng)屬性值為空或不匹配havingValue設(shè)定值2024-09-09源碼解析Spring 數(shù)據(jù)庫(kù)異常抽理知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享了關(guān)于源碼解析Spring 數(shù)據(jù)庫(kù)異常抽理知識(shí)點(diǎn)內(nèi)容,對(duì)此有需要的朋友們學(xué)習(xí)參考下。2019-05-05使用Java實(shí)現(xiàn)簡(jiǎn)單搭建內(nèi)網(wǎng)穿透
內(nèi)網(wǎng)穿透是一種網(wǎng)絡(luò)技術(shù),適用于需要遠(yuǎn)程訪問(wèn)本地部署服務(wù)的場(chǎng)景,本文主要為大家介紹了如何使用Java實(shí)現(xiàn)簡(jiǎn)單搭建內(nèi)網(wǎng)穿透,感興趣的可以了解下2024-02-02java普通項(xiàng)目讀取不到resources目錄下資源文件的解決辦法
這篇文章主要給大家介紹了關(guān)于java普通項(xiàng)目讀取不到resources目錄下資源文件的解決辦法,Web項(xiàng)目中應(yīng)該經(jīng)常有這樣的需求,在maven項(xiàng)目的resources目錄下放一些文件,比如一些配置文件,資源文件等,需要的朋友可以參考下2023-09-09Spring Cloud Gateway實(shí)現(xiàn)灰度發(fā)布方案
灰度發(fā)布是在微服務(wù)中的表現(xiàn)為同一服務(wù)同時(shí)上線不同版本,讓一部分用戶使用新版本來(lái)驗(yàn)證新特性,如果驗(yàn)證沒(méi)有問(wèn)題,則將所有用戶都遷移到新版本上,本文就來(lái)介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下2023-12-12