使用SpringBoot獲取所有接口的路由
SpringBoot獲取所有接口的路由
@Autowired
WebApplicationContext applicationContext;
@RequestMapping(value = "v1/getAllUrl", method = RequestMethod.POST)
public Object getAllUrl() {
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
// 獲取url與類和方法的對應(yīng)信息
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
// List<String> urlList = new ArrayList<>();
// for (RequestMappingInfo info : map.keySet()) {
// // 獲取url的Set集合,一個方法可能對應(yīng)多個url
// Set<String> patterns = info.getPatternsCondition().getPatterns();
//
// for (String url : patterns) {
// urlList.add(url);
// }
// }
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
Map<String, String> map1 = new HashMap<String, String>();
RequestMappingInfo info = m.getKey();
HandlerMethod method = m.getValue();
PatternsRequestCondition p = info.getPatternsCondition();
for (String url : p.getPatterns()) {
map1.put("url", url);
}
map1.put("className", method.getMethod().getDeclaringClass().getName()); // 類名
map1.put("method", method.getMethod().getName()); // 方法名
RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
for (RequestMethod requestMethod : methodsCondition.getMethods()) {
map1.put("type", requestMethod.toString());
}
list.add(map1);
}
Springboot部分路由生效
問題記錄
項目新增接口"foo",始終不生效,經(jīng)排查發(fā)現(xiàn)controller層的@RequestMaping(value=“test”)統(tǒng)一加了基礎(chǔ)路徑"test",我新增的接口注解為@PostMappinp(“test/foo),導(dǎo)致生成的路由為"test/test/foo”, 調(diào)用地址為"test/foo",所以報了404。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
一文詳解如何使用線程池來優(yōu)化我們的應(yīng)用程序
線程池是一種工具,但并不是適用于所有場景。在使用線程池時,我們需要根據(jù)應(yīng)用程序的性質(zhì)、計算資源的可用性和應(yīng)用程序的需求進(jìn)行適當(dāng)?shù)呐渲?。本文主要介紹了如何使用線程池來優(yōu)化我們的應(yīng)用程序,需要的可以參考一下2023-04-04
idea 查看一個類的所有子類以及子類的子類并以層級關(guān)系顯示
這篇文章主要介紹了idea 查看一個類的所有子類以及子類的子類并以層級關(guān)系顯示,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Spring之SseEmitter實現(xiàn)讓你的進(jìn)度條實時更新
Spring SseEmitter是一種實現(xiàn)服務(wù)器端推送事件(SSE)的機(jī)制,支持單向通信,適用于實時數(shù)據(jù)傳輸需求,通過代碼示例和應(yīng)用場景分析,展示了如何在服務(wù)端和客戶端使用SseEmitter進(jìn)行實時數(shù)據(jù)推送2025-02-02
java書店系統(tǒng)畢業(yè)設(shè)計 用戶模塊(3)
這篇文章主要介紹了java書店系統(tǒng)畢業(yè)設(shè)計,第三步系統(tǒng)總體設(shè)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

