SpringBoot返回所有接口详细信息的方法详解

 更新时间:2025年04月11日 08:42:42   作者:小u  
这篇文章主要介绍了SpringBoot返回所有接口详细信息的方法,简单来说就是我们通过访问一个接口能看到我们所有的API接口的数量,以及路径和请求方法,文中有详细的代码供大家参考,需要的朋友可以参考下

springboot返回所有接口详细信息

简单来说

就是我们通过访问一个接口能看到我们所有的API接口的数量。

以及路径和请求方法。

这个是我今天再做一个项目的首页的时候。

前端的设计是有一个这样的需求

因此这个数据需要我们从后台来进行一个动态的获取。

这里我们所需要用到的就是

spring-boot-starter-actuator

首先导入依赖

<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 项目中所有控制器方法的路径和请求方式,然后把这些信息组织成一个列表,返回给用户。通过这种方式,开发者可以查看当前 Spring Boot 项目中的所有公开 API 接口及其支持的请求方法。

这一过程的核心依赖是 Spring Boot 的 RequestMappingHandlerMapping 类,该类负责管理所有请求路径的映射,能够获取每个路径的具体信息。

Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
  • handlerMapping.getHandlerMethods() 通过 Spring 的 RequestMappingHandlerMapping 类获取所有已经注册的请求映射信息。
  • 这里返回的是一个 MapkeyRequestMappingInfo(包含了路径和请求方法的相关信息),valueHandlerMethod(指向处理该请求的控制器方法)。

后面的就是在对返回的数据进行一个处理。

之后就会返回一个这样的json

这样就完成了我们的需求。

需要注意的是这段代码

if (info.getPathPatternsCondition() != null) {
    info.getPathPatternsCondition().getPatterns()
        .forEach(p -> paths.add(p.getPatternString()));
}

Spring Boot 3.x 的新方式:使用 getPathPatternsCondition() 获取路径集合(Pattern 类型),然后转成字符串加到 paths 里。

Spring Boot 2.x 是用 getPatternsCondition(),在 3.x 中已经废弃。

后面我贴了一个兼容版本,既可以兼容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返回所有接口详细信息的方法详解的详细内容,更多关于SpringBoot返回接口信息的资料请关注脚本之家其它相关文章!

相关文章

  • 解决使用httpclient传递json数据乱码的问题

    解决使用httpclient传递json数据乱码的问题

    这篇文章主要介绍了解决使用httpclient传递json数据乱码的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • Java中DTO与Entity拷贝转换的方法小结

    Java中DTO与Entity拷贝转换的方法小结

    在 Java 开发中,DTO(Data Transfer Object)和 Entity(实体类)是常见的两种数据模型,本文将介绍几种常见的工具类和自定义方式来实现这种转换,感兴趣的可以了解下
    2025-02-02
  • Springboot集成百度地图实现定位打卡的示例代码

    Springboot集成百度地图实现定位打卡的示例代码

    本文主要介绍了Springboot集成百度地图实现定位打卡的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • Java调用第三方http接口的常用方式总结

    Java调用第三方http接口的常用方式总结

    这篇文章主要介绍了Java调用第三方http接口的常用方式总结,具有很好的参考价值,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • Java实现数字金额转换为中文大写金额的完整指南与优化技巧

    Java实现数字金额转换为中文大写金额的完整指南与优化技巧

    在金融、财务和商务系统中,将数字金额转换为中文大写金额是一项常见需求,中文大写金额能有效避免篡改和歧义,是合同、发票等正式场景的规范要求,本文将通过Java实现这一功能,需要的朋友可以参考下
    2025-12-12
  • SpringBoot实现多文件上传的详细示例代码

    SpringBoot实现多文件上传的详细示例代码

    文件上传中并没有什么太多的知识点,下面这篇文章主要给大家介绍了关于SpringBoot实现多文件上传的详细示例,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • springboot 使用 minio的示例代码

    springboot 使用 minio的示例代码

    Minio是Apcche旗下的一款开源的轻量级文件服务器,基于对象存储,协议是基于Apache License v2.0,开源可用于商务,本文给大家介绍下springboot 使用 minio的示例代码,感兴趣的朋友看看吧
    2022-03-03
  • 详解eclipse创建maven项目实现动态web工程完整示例

    详解eclipse创建maven项目实现动态web工程完整示例

    这篇文章主要介绍了详解eclipse创建maven项目实现动态web工程完整示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • java获取反射机制的3种方法总结

    java获取反射机制的3种方法总结

    这篇文章主要给大家介绍了关于java获取反射机制的3种方法,文中通过示例代码介绍的非常详细,对大家学习或者使用java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • MyBatis关于二级缓存问题

    MyBatis关于二级缓存问题

    本篇文章主要介绍了MyBatis关于二级缓存问题,二级缓存是Mapper级别的缓存,多个sqlSession操作同一个Mapper,其二级缓存是可以共享的。
    2017-03-03

最新评论