SpringBoot返回所有接口詳細信息的方法詳解
springboot返回所有接口詳細信息
簡單來說
就是我們通過訪問一個接口能看到我們所有的API接口的數(shù)量。
以及路徑和請求方法。
這個是我今天再做一個項目的首頁的時候。
前端的設(shè)計是有一個這樣的需求

因此這個數(shù)據(jù)需要我們從后臺來進行一個動態(tài)的獲取。
這里我們所需要用到的就是
spring-boot-starter-actuator
首先導(dǎo)入依賴
<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的寫法。
這個代碼的核心原理就是
通過反射獲取 Spring Boot 項目中所有控制器方法的路徑和請求方式,然后把這些信息組織成一個列表,返回給用戶。通過這種方式,開發(fā)者可以查看當(dāng)前 Spring Boot 項目中的所有公開 API 接口及其支持的請求方法。
這一過程的核心依賴是 Spring Boot 的 RequestMappingHandlerMapping 類,該類負責(zé)管理所有請求路徑的映射,能夠獲取每個路徑的具體信息。
Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
handlerMapping.getHandlerMethods()通過 Spring 的RequestMappingHandlerMapping類獲取所有已經(jīng)注冊的請求映射信息。- 這里返回的是一個
Map,key是RequestMappingInfo(包含了路徑和請求方法的相關(guān)信息),value是HandlerMethod(指向處理該請求的控制器方法)。
后面的就是在對返回的數(shù)據(jù)進行一個處理。
之后就會返回一個這樣的json

這樣就完成了我們的需求。
需要注意的是這段代碼
if (info.getPathPatternsCondition() != null) {
info.getPathPatternsCondition().getPatterns()
.forEach(p -> paths.add(p.getPatternString()));
}
Spring Boot 3.x 的新方式:使用 getPathPatternsCondition() 獲取路徑集合(Pattern 類型),然后轉(zhuǎn)成字符串加到 paths 里。
Spring Boot 2.x 是用 getPatternsCondition(),在 3.x 中已經(jīng)廢棄。
后面我貼了一個兼容版本,既可以兼容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返回所有接口詳細信息的方法詳解的詳細內(nèi)容,更多關(guān)于SpringBoot返回接口信息的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring?Cloud?Stream消息驅(qū)動組件使用方法介紹
Spring?Cloud?Stream?消息驅(qū)動組件幫助我們更快速,更方便,更友好的去構(gòu)建消息驅(qū)動微服務(wù)的。當(dāng)時定時任務(wù)和消息驅(qū)動的?個對比。消息驅(qū)動:基于消息機制做一些事情2022-09-09
Java實現(xiàn)多數(shù)據(jù)源的幾種方式總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于Java實現(xiàn)多數(shù)據(jù)源的幾種方式,最近項目中的工作流需要查詢多個數(shù)據(jù)源的數(shù)據(jù),數(shù)據(jù)源可能是不同種類的,需要的朋友可以參考下2023-08-08
SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析
這篇文章主要介紹了SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
Mybatis參數(shù)(Parameters)傳遞方式
這篇文章主要介紹了Mybatis參數(shù)(Parameters)傳遞方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Spring Boot中操作使用Redis實現(xiàn)詳解
Spring Boot與Redis結(jié)合使用,通過使用Spring Data Redis來實現(xiàn)對Redis的操作,實現(xiàn)數(shù)據(jù)緩存和高效存儲,提高應(yīng)用程序的性能和響應(yīng)速度。可以利用Spring Boot自帶的Redis Starter方便地集成和配置Redis2023-04-04

