SpringBoot返回所有接口詳細(xì)信息的方法詳解
springboot返回所有接口詳細(xì)信息
簡(jiǎn)單來(lái)說(shuō)
就是我們通過(guò)訪(fǎng)問(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)文章
Spring?Cloud?Stream消息驅(qū)動(dòng)組件使用方法介紹
Spring?Cloud?Stream?消息驅(qū)動(dòng)組件幫助我們更快速,更方便,更友好的去構(gòu)建消息驅(qū)動(dòng)微服務(wù)的。當(dāng)時(shí)定時(shí)任務(wù)和消息驅(qū)動(dòng)的?個(gè)對(duì)比。消息驅(qū)動(dòng):基于消息機(jī)制做一些事情2022-09-09
Java實(shí)現(xiàn)多數(shù)據(jù)源的幾種方式總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于Java實(shí)現(xiàn)多數(shù)據(jù)源的幾種方式,最近項(xiàng)目中的工作流需要查詢(xún)多個(gè)數(shù)據(jù)源的數(shù)據(jù),數(shù)據(jù)源可能是不同種類(lèi)的,需要的朋友可以參考下2023-08-08
SpringMVC自定義類(lèi)型轉(zhuǎn)換器實(shí)現(xiàn)解析
這篇文章主要介紹了SpringMVC自定義類(lèi)型轉(zhuǎn)換器實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
java 中設(shè)計(jì)模式(值對(duì)象)的實(shí)例詳解
這篇文章主要介紹了java 中設(shè)計(jì)模式(值對(duì)象)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
Mybatis參數(shù)(Parameters)傳遞方式
這篇文章主要介紹了Mybatis參數(shù)(Parameters)傳遞方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Spring Boot中操作使用Redis實(shí)現(xiàn)詳解
Spring Boot與Redis結(jié)合使用,通過(guò)使用Spring Data Redis來(lái)實(shí)現(xiàn)對(duì)Redis的操作,實(shí)現(xiàn)數(shù)據(jù)緩存和高效存儲(chǔ),提高應(yīng)用程序的性能和響應(yīng)速度。可以利用Spring Boot自帶的Redis Starter方便地集成和配置Redis2023-04-04
springboot3?redis?常用操作工具類(lèi)詳解
本文詳細(xì)介紹了Spring Boot 3中使用Spring Data Redis進(jìn)行Redis操作的工具類(lèi)實(shí)現(xiàn),該工具類(lèi)涵蓋了字符串、哈希、列表、集合和有序集合等常用功能,感興趣的朋友一起看看吧2025-01-01

