SpringBoot返回所有接口詳細(xì)信息的方法詳解
springboot返回所有接口詳細(xì)信息
簡(jiǎn)單來(lái)說(shuō)
就是我們通過(guò)訪問(wèn)一個(gè)接口能看到我們所有的API接口的數(shù)量。
以及路徑和請(qǐng)求方法。
這個(gè)是我今天再做一個(gè)項(xiàng)目的首頁(yè)的時(shí)候。
前端的設(shè)計(jì)是有一個(gè)這樣的需求

因此這個(gè)數(shù)據(jù)需要我們從后臺(tái)來(lái)進(jìn)行一個(gè)動(dòng)態(tài)的獲取。
這里我們所需要用到的就是
spring-boot-starter-actuator
首先導(dǎo)入依賴(lài)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.context.ApplicationContext;
import java.util.*;
@RestController
@RequestMapping("/uapi/api-list")
@Tag(name = "API列表", description = "API列表")
public class ApiListController {
private final RequestMappingHandlerMapping handlerMapping;
@Autowired
public ApiListController(ApplicationContext context) {
this.handlerMapping = context.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
}
@GetMapping
@Operation(summary = "獲取所有API列表")
public Map<String, Object> listAllApi() {
Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
List<Map<String, String>> apiList = new ArrayList<>();
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
RequestMappingInfo info = entry.getKey();
Set<String> paths = new HashSet<>();
// ? 只使用 Spring Boot 3 推薦方式
if (info.getPathPatternsCondition() != null) {
info.getPathPatternsCondition().getPatterns()
.forEach(p -> paths.add(p.getPatternString()));
}
Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
for (String path : paths) {
if (methods.isEmpty()) {
apiList.add(Map.of("method", "ANY", "path", path));
} else {
for (RequestMethod method : methods) {
apiList.add(Map.of("method", method.name(), "path", path));
}
}
}
}
Map<String, Object> result = new HashMap<>();
result.put("count", apiList.size());
result.put("apis", apiList);
return result;
}
}
上面貼出的是springboot3的寫(xiě)法。
這個(gè)代碼的核心原理就是
通過(guò)反射獲取 Spring Boot 項(xiàng)目中所有控制器方法的路徑和請(qǐng)求方式,然后把這些信息組織成一個(gè)列表,返回給用戶(hù)。通過(guò)這種方式,開(kāi)發(fā)者可以查看當(dāng)前 Spring Boot 項(xiàng)目中的所有公開(kāi) API 接口及其支持的請(qǐng)求方法。
這一過(guò)程的核心依賴(lài)是 Spring Boot 的 RequestMappingHandlerMapping 類(lèi),該類(lèi)負(fù)責(zé)管理所有請(qǐng)求路徑的映射,能夠獲取每個(gè)路徑的具體信息。
Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
handlerMapping.getHandlerMethods()通過(guò) Spring 的RequestMappingHandlerMapping類(lèi)獲取所有已經(jīng)注冊(cè)的請(qǐng)求映射信息。- 這里返回的是一個(gè)
Map,key是RequestMappingInfo(包含了路徑和請(qǐng)求方法的相關(guān)信息),value是HandlerMethod(指向處理該請(qǐng)求的控制器方法)。
后面的就是在對(duì)返回的數(shù)據(jù)進(jìn)行一個(gè)處理。
之后就會(huì)返回一個(gè)這樣的json

這樣就完成了我們的需求。
需要注意的是這段代碼
if (info.getPathPatternsCondition() != null) {
info.getPathPatternsCondition().getPatterns()
.forEach(p -> paths.add(p.getPatternString()));
}
Spring Boot 3.x 的新方式:使用 getPathPatternsCondition() 獲取路徑集合(Pattern 類(lèi)型),然后轉(zhuǎn)成字符串加到 paths 里。
Spring Boot 2.x 是用 getPatternsCondition(),在 3.x 中已經(jīng)廢棄。
后面我貼了一個(gè)兼容版本,既可以兼容springboot3也可以兼容springboot2
@GetMapping("/api-list")
public Map<String, Object> listAllApi() {
Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
List<Map<String, String>> apiList = new ArrayList<>();
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
RequestMappingInfo info = entry.getKey();
Set<String> paths = new HashSet<>();
// Spring Boot 2.x
if (info.getPatternsCondition() != null) {
paths.addAll(info.getPatternsCondition().getPatterns());
}
// Spring Boot 3.x
if (info.getPathPatternsCondition() != null) {
info.getPathPatternsCondition().getPatterns()
.forEach(p -> paths.add(p.getPatternString()));
}
Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
for (String path : paths) {
if (methods.isEmpty()) {
apiList.add(Map.of("method", "ANY", "path", path));
} else {
for (RequestMethod method : methods) {
apiList.add(Map.of("method", method.name(), "path", path));
}
}
}
}
Map<String, Object> result = new HashMap<>();
result.put("count", apiList.size());
result.put("apis", apiList);
return result;
}
以上就是SpringBoot返回所有接口詳細(xì)信息的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot返回接口信息的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決使用httpclient傳遞json數(shù)據(jù)亂碼的問(wèn)題
這篇文章主要介紹了解決使用httpclient傳遞json數(shù)據(jù)亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
Java中DTO與Entity拷貝轉(zhuǎn)換的方法小結(jié)
在?Java?開(kāi)發(fā)中,DTO(Data?Transfer?Object)和?Entity(實(shí)體類(lèi))是常見(jiàn)的兩種數(shù)據(jù)模型,本文將介紹幾種常見(jiàn)的工具類(lèi)和自定義方式來(lái)實(shí)現(xiàn)這種轉(zhuǎn)換,感興趣的可以了解下2025-02-02
Springboot集成百度地圖實(shí)現(xiàn)定位打卡的示例代碼
本文主要介紹了Springboot集成百度地圖實(shí)現(xiàn)定位打卡的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
Java調(diào)用第三方http接口的常用方式總結(jié)
這篇文章主要介紹了Java調(diào)用第三方http接口的常用方式總結(jié),具有很好的參考價(jià)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Java實(shí)現(xiàn)數(shù)字金額轉(zhuǎn)換為中文大寫(xiě)金額的完整指南與優(yōu)化技巧
在金融、財(cái)務(wù)和商務(wù)系統(tǒng)中,將數(shù)字金額轉(zhuǎn)換為中文大寫(xiě)金額是一項(xiàng)常見(jiàn)需求,中文大寫(xiě)金額能有效避免篡改和歧義,是合同、發(fā)票等正式場(chǎng)景的規(guī)范要求,本文將通過(guò)Java實(shí)現(xiàn)這一功能,需要的朋友可以參考下2025-12-12
SpringBoot實(shí)現(xiàn)多文件上傳的詳細(xì)示例代碼
文件上傳中并沒(méi)有什么太多的知識(shí)點(diǎn),下面這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)多文件上傳的詳細(xì)示例,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
詳解eclipse創(chuàng)建maven項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)web工程完整示例
這篇文章主要介紹了詳解eclipse創(chuàng)建maven項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)web工程完整示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
MyBatis關(guān)于二級(jí)緩存問(wèn)題
本篇文章主要介紹了MyBatis關(guān)于二級(jí)緩存問(wèn)題,二級(jí)緩存是Mapper級(jí)別的緩存,多個(gè)sqlSession操作同一個(gè)Mapper,其二級(jí)緩存是可以共享的。2017-03-03

