亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot返回所有接口詳細(xì)信息的方法詳解

 更新時間:2025年04月11日 08:42:42   作者:小u  
這篇文章主要介紹了SpringBoot返回所有接口詳細(xì)信息的方法,簡單來說就是我們通過訪問一個接口能看到我們所有的API接口的數(shù)量,以及路徑和請求方法,文中有詳細(xì)的代碼供大家參考,需要的朋友可以參考下

springboot返回所有接口詳細(xì)信息

簡單來說

就是我們通過訪問一個接口能看到我們所有的API接口的數(shù)量。

以及路徑和請求方法。

這個是我今天再做一個項目的首頁的時候。

前端的設(shè)計是有一個這樣的需求

因此這個數(shù)據(jù)需要我們從后臺來進(jìn)行一個動態(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 類,該類負(fù)責(zé)管理所有請求路徑的映射,能夠獲取每個路徑的具體信息。

Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
  • handlerMapping.getHandlerMethods() 通過 Spring 的 RequestMappingHandlerMapping 類獲取所有已經(jīng)注冊的請求映射信息。
  • 這里返回的是一個 Map,keyRequestMappingInfo(包含了路徑和請求方法的相關(guān)信息),valueHandlerMethod(指向處理該請求的控制器方法)。

后面的就是在對返回的數(shù)據(jù)進(jìn)行一個處理。

之后就會返回一個這樣的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返回所有接口詳細(xì)信息的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot返回接口信息的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring?Cloud?Stream消息驅(qū)動組件使用方法介紹

    Spring?Cloud?Stream消息驅(qū)動組件使用方法介紹

    Spring?Cloud?Stream?消息驅(qū)動組件幫助我們更快速,更方便,更友好的去構(gòu)建消息驅(qū)動微服務(wù)的。當(dāng)時定時任務(wù)和消息驅(qū)動的?個對比。消息驅(qū)動:基于消息機(jī)制做一些事情
    2022-09-09
  • java 示例講解循環(huán)語句的使用

    java 示例講解循環(huán)語句的使用

    順序結(jié)構(gòu)的程序語句只能被執(zhí)行一次。如果您想要同樣的操作執(zhí)行多次,就需要使用循環(huán)結(jié)構(gòu),循環(huán)結(jié)構(gòu)就是在循環(huán)條件滿足的情況下,反復(fù)執(zhí)行特定代碼
    2022-04-04
  • Java實現(xiàn)多數(shù)據(jù)源的幾種方式總結(jié)

    Java實現(xiàn)多數(shù)據(jù)源的幾種方式總結(jié)

    這篇文章主要給大家總結(jié)介紹了關(guān)于Java實現(xiàn)多數(shù)據(jù)源的幾種方式,最近項目中的工作流需要查詢多個數(shù)據(jù)源的數(shù)據(jù),數(shù)據(jù)源可能是不同種類的,需要的朋友可以參考下
    2023-08-08
  • 關(guān)于Java中重定向傳參與取值

    關(guān)于Java中重定向傳參與取值

    這篇文章主要介紹了Java中重定向傳參與取值問題,重定向不僅可以重定向到當(dāng)前應(yīng)用程序中的其他資源,還可以重定向到同一個站點(diǎn)上的其他應(yīng)用程序中的資源,甚至是使用絕對URL重定向到其他站點(diǎn)的資源,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析

    SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析

    這篇文章主要介紹了SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • java 中設(shè)計模式(值對象)的實例詳解

    java 中設(shè)計模式(值對象)的實例詳解

    這篇文章主要介紹了java 中設(shè)計模式(值對象)的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • Mybatis參數(shù)(Parameters)傳遞方式

    Mybatis參數(shù)(Parameters)傳遞方式

    這篇文章主要介紹了Mybatis參數(shù)(Parameters)傳遞方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • java 中同步方法和同步代碼塊的區(qū)別詳解

    java 中同步方法和同步代碼塊的區(qū)別詳解

    這篇文章主要介紹了java 中同步方法和同步代碼塊的區(qū)別是什么的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Spring Boot中操作使用Redis實現(xiàn)詳解

    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方便地集成和配置Redis
    2023-04-04
  • springboot3?redis?常用操作工具類詳解

    springboot3?redis?常用操作工具類詳解

    本文詳細(xì)介紹了Spring Boot 3中使用Spring Data Redis進(jìn)行Redis操作的工具類實現(xiàn),該工具類涵蓋了字符串、哈希、列表、集合和有序集合等常用功能,感興趣的朋友一起看看吧
    2025-01-01

最新評論