重要
:版本很重要,先说一下版本,版本不一样,可能获取结果也不一样
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