使用SpringBoot獲取所有接口的路由
更新時間:2021年09月11日 11:00:57 作者:情陌人灬已不在
這篇文章主要介紹了使用SpringBoot獲取所有接口的路由方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
SpringBoot獲取所有接口的路由
@Autowired
WebApplicationContext applicationContext;
@RequestMapping(value = "v1/getAllUrl", method = RequestMethod.POST)
public Object getAllUrl() {
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
// 獲取url與類和方法的對應信息
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
// List<String> urlList = new ArrayList<>();
// for (RequestMappingInfo info : map.keySet()) {
// // 獲取url的Set集合,一個方法可能對應多個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",始終不生效,經排查發(fā)現controller層的@RequestMaping(value=“test”)統(tǒng)一加了基礎路徑"test",我新增的接口注解為@PostMappinp(“test/foo),導致生成的路由為"test/test/foo”, 調用地址為"test/foo",所以報了404。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring?Boot?3.1中整合Spring?Security和Keycloak的方法
本文介紹在最新的SpringBoot3.1版本之下,如何將Keycloak和Spring?Security一起跑起來,文中結合實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2023-06-06
解決Spring?Security集成knife4j訪問接口文檔出現403的問題
這篇文章主要給大家介紹了如何解決Spring?Security集成knife4j訪問接口文檔出現403的問題,文中有詳細的解決方案,有需要的朋友可以參考閱讀下2023-07-07
使用LambdaQueryWrapper動態(tài)加過濾條件?動態(tài)Lambda
這篇文章主要介紹了使用LambdaQueryWrapper動態(tài)加過濾條件?動態(tài)Lambda,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01
Java實現將word轉換為html的方法示例【doc與docx格式】
這篇文章主要介紹了Java實現將word轉換為html的方法,結合實例形式分析了java針對doc與docx格式文件的相關轉換操作技巧,需要的朋友可以參考下2019-03-03

