当前位置: 首页 > wzjs >正文

wordpress 调用模板文件上海做网站优化价格

wordpress 调用模板文件,上海做网站优化价格,云南网招聘,个人在国外网站做电商springboot返回所有接口详细信息 简单来说 就是我们通过访问一个接口能看到我们所有的API接口的数量。 以及路径和请求方法。 这个是我今天再做一个项目的首页的时候。 前端的设计是有一个这样的需求 因此这个数据需要我们从后台来进行一个动态的获取。 这里我们所需要用…

springboot返回所有接口详细信息

简单来说

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

以及路径和请求方法。

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

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

image-20250410163506837

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

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

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;@Autowiredpublic 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

image-20250410164002947

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

需要注意的是这段代码

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.xif (info.getPatternsCondition() != null) {paths.addAll(info.getPatternsCondition().getPatterns());}// Spring Boot 3.xif (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;
}
http://www.dtcms.com/wzjs/589292.html

相关文章:

  • 室内设计招聘网站有哪些西安网站开发托管代运营
  • 如何申请cn域名做网站中国万网域名官网
  • 网站加载慢手机浏览器下载app
  • 免费建站哪个好计算机网络 网站
  • 怎么做室内设计公司网站关于三亚的网页设计
  • 金华网站建设建设设计网站app开发公司怎么学东西
  • 做网站的多钱wordpress旅游社区
  • 特效视频素材网站wordpress不同栏目使用不同模板
  • 网站制作:网推宝|百度网站制作需要多长时间
  • 风险的网站怎么出现微信朋友圈广告如何投放
  • 一台主机做两个网站有创意的个人网站名字
  • 兰州网站建设王道下拉強网站运营方案设计
  • 科技公司网站建设方案书模板正规的网店代运营
  • 凡科做的网站为什么搜不到河南省住建厅网站官网
  • 建设项目管理公司网站营销型网站盈利模式
  • 北京效果好的网站推广asp.net学校网站整站系统源码
  • 教育网站建设网站律师事务所咨询免费
  • 用vs2010做网站教程网络营销策划书ppt
  • 手机wap网站特效军事最新消息新闻
  • 移动网站建设厂家网站搭建赚钱吗
  • 一级a做爰片免费网站天天看手机网站建设计
  • 纯静态 网站网站通知模板
  • 个人网站备案核验单织梦做的网站老是被黑
  • 网站建设咨询云尚网络360全景预览wordpress插件
  • 网站框架设计模板做软件界面的网站
  • 开发网站制作自己电脑做服务器搭建网站有域名
  • 做的精美的门户网站推荐华侨城网站开发
  • 桂林广告公司网站建设深圳做英文网站公司
  • 电子商务 网站设计做网站做得好的公司
  • 教做美食网站源码win7 iis 添加网站