重要
:版本很重要,先说一下版本,版本不一样,可能获取结果也不一样
spring-boot 2.7.7
java 1.8复制
定义一个查看路由的数据结构
package com.example.demo.entity;
import lombok.Data;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Set;
@Data
public class Route {
/**
* 类名
*/
private String className;
/**
* 方法名
*/
private String methodName;
/**
* 匹配规则
*/
private Set<String> patterns;
/**
* 请求方式 GET/POST
*/
private Set<RequestMethod> methods;
}复制
获取SpringBoot路由映射关系
package com.example.demo.config;
import com.example.demo.entity.Route;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.util.pattern.PathPattern;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 获取SpringBoot路由映射关系
*/
@Component
public class RouteConfig {
@Autowired
private WebApplicationContext applicationContext;
/**
* 获取所有路由映射关系
*
* @return
*/
public List<Route> getAllRouteMapping() {
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
// 获取url与类和方法的对应信息
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
List<Route> list = new ArrayList<>();
// 遍历
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : map.entrySet()) {
Route route = this.getRoute(entry);
list.add(route);
}
return list;
}
/**
* 获取路由对象
*
* @return
*/
public Route getRoute(Map.Entry<RequestMappingInfo, HandlerMethod> entry) {
Route route = new Route();
RequestMappingInfo info = entry.getKey();
// 请求路径
PathPatternsRequestCondition requestCondition = info.getPathPatternsCondition();
if (requestCondition != null) {
Set<PathPattern> patterns = requestCondition.getPatterns();
if (patterns != null) {
route.setPatterns(patterns
.stream()
.map(PathPattern::getPatternString)
.collect(Collectors.toSet()));
}
}
// 请求方法
RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
if (methodsCondition != null) {
route.setMethods(methodsCondition.getMethods());
}
HandlerMethod method = entry.getValue();
// 类名
route.setClassName(method.getMethod().getDeclaringClass().getName());
// 方法名
route.setMethodName(method.getMethod().getName());
return route;
}
}复制
通过控制器输出路由映射数据
package com.example.demo.controller;
import com.example.demo.config.RouteConfig;
import com.example.demo.entity.Route;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api")
public class AppIndexController {
@Autowired
private RouteConfig routeConfig;
@GetMapping("/route")
public List<Route> route() {
return routeConfig.getAllRouteMapping();
}
}复制
访问路径:http://localhost:8080/api/route
输出数据如下
[
{
"className": "org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController",
"methodName": "errorHtml",
"patterns": [
"/error"
],
"methods": []
},
{
"className": "org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController",
"methodName": "error",
"patterns": [
"/error"
],
"methods": []
},
{
"className": "com.example.demo.controller.AppIndexController",
"methodName": "route",
"patterns": [
"/api/route"
],
"methods": [
"GET"
]
}
]复制
完整代码: https://github.com/mouday/spring-boot-demo
参考
SpringBoot获取所有接口的路由
文章转载自Coding Big Tree,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
Java萌新修炼手册②:Hello Worldの108种写法——从入门到入坟!
让天下没有难学的编程
50次阅读
2025-04-21 10:34:36
Java萌新修炼手册⑥:面向对象の修仙奥义——从"散修"到"宗门大佬"的基因飞升!
让天下没有难学的编程
47次阅读
2025-04-25 10:10:40
从 Java 到 Go:面向对象的巨人与云原生的轻骑兵
京东云开发者
37次阅读
2025-04-25 11:41:37
Java萌新修炼手册⑤:数组の千层套路——从"鸽子笼"到"摩天楼"的进阶之路!
让天下没有难学的编程
35次阅读
2025-04-25 10:10:41
Java萌新修炼手册④:流程控制の三十六计——让代码学会"见风使舵"!
让天下没有难学的编程
29次阅读
2025-04-23 14:33:55
万字解析:攻克秒级抖动难题,腾讯云MongoDB路由底座优化实践
腾讯云数据库
27次阅读
2025-04-15 09:49:58
5.8 | Java 23:JavaDoc 重大升级,支持Markdown文档注释
严少安
25次阅读
2025-05-12 00:34:42
Java萌新修炼手册①:开局一把JDK,环境搭建全靠浪!
让天下没有难学的编程
23次阅读
2025-04-21 10:34:37
java浅拷贝BeanUtils.copyProperties引发的RPC异常
京东云开发者
19次阅读
2025-04-30 17:10:50
分库分表后悔了?你可能只是没设计好路由方案
解压泡泡糖
17次阅读
2025-05-02 08:02:38