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返回接口信息的资料请关注脚本之家其它相关文章!

相关文章

  • Spring Cloud Stream消息驱动组件使用方法介绍

    Spring Cloud Stream消息驱动组件使用方法介绍

    Spring Cloud Stream 消息驱动组件帮助我们更快速,更方便,更友好的去构建消息驱动微服务的。当时定时任务和消息驱动的⼀个对比。消息驱动:基于消息机制做一些事情
    2022-09-09
  • java 示例讲解循环语句的使用

    java 示例讲解循环语句的使用

    顺序结构的程序语句只能被执行一次。如果您想要同样的操作执行多次,就需要使用循环结构,循环结构就是在循环条件满足的情况下,反复执行特定代码
    2022-04-04
  • Java实现多数据源的几种方式总结

    Java实现多数据源的几种方式总结

    这篇文章主要给大家总结介绍了关于Java实现多数据源的几种方式,最近项目中的工作流需要查询多个数据源的数据,数据源可能是不同种类的,需要的朋友可以参考下
    2023-08-08
  • 关于Java中重定向传参与取值

    关于Java中重定向传参与取值

    这篇文章主要介绍了Java中重定向传参与取值问题,重定向不仅可以重定向到当前应用程序中的其他资源,还可以重定向到同一个站点上的其他应用程序中的资源,甚至是使用绝对URL重定向到其他站点的资源,本文给大家介绍的非常详细,需要的朋友可以参考下
    2023-05-05
  • SpringMVC自定义类型转换器实现解析

    SpringMVC自定义类型转换器实现解析

    这篇文章主要介绍了SpringMVC自定义类型转换器实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • java 中设计模式(值对象)的实例详解

    java 中设计模式(值对象)的实例详解

    这篇文章主要介绍了java 中设计模式(值对象)的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-09-09
  • Mybatis参数(Parameters)传递方式

    Mybatis参数(Parameters)传递方式

    这篇文章主要介绍了Mybatis参数(Parameters)传递方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • java 中同步方法和同步代码块的区别详解

    java 中同步方法和同步代码块的区别详解

    这篇文章主要介绍了java 中同步方法和同步代码块的区别是什么的相关资料,需要的朋友可以参考下
    2017-02-02
  • Spring Boot中操作使用Redis实现详解

    Spring Boot中操作使用Redis实现详解

    Spring Boot与Redis结合使用,通过使用Spring Data Redis来实现对Redis的操作,实现数据缓存和高效存储,提高应用程序的性能和响应速度。可以利用Spring Boot自带的Redis Starter方便地集成和配置Redis
    2023-04-04
  • springboot3 redis 常用操作工具类详解

    springboot3 redis 常用操作工具类详解

    本文详细介绍了Spring Boot 3中使用Spring Data Redis进行Redis操作的工具类实现,该工具类涵盖了字符串、哈希、列表、集合和有序集合等常用功能,感兴趣的朋友一起看看吧
    2025-01-01

最新评论