spring boot 获取Controller URL
@Autowired
private RequestMappingHandlerMapping mappingHandlerMapping;
@PostConstruct
public void urlInfo(){
log.info(">>> request info ....");
List<Map<String,String>> map = new ArrayList<>();
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : mappingHandlerMapping.getHandlerMethods().entrySet()) {
Map<String,String> m = new HashMap<>();
RequestMappingInfo requestMappingInfo = entry.getKey();
Set<PathPattern> patterns = requestMappingInfo.getPathPatternsCondition().getPatterns();
for (PathPattern pattern : patterns) {
m.put("url", pattern.getPatternString());
}
HandlerMethod handlerMethod = entry.getValue();
m.put("method", handlerMethod.getMethod().getName());
m.put("class",handlerMethod.getMethod().getDeclaringClass().getName());
RequestMethodsRequestCondition methodsCondition = requestMappingInfo.getMethodsCondition();
for (RequestMethod method : methodsCondition.getMethods()) {
m.put("type", method.toString());
}
map.add(m);
}
System.out.println(JSON.toJSONString(map));
}
结果:
[
{
"method": "index",
"type": "GET",
"class": "com.example.bootkafka.controller.IndexController",
"url": "/test/abc"
},
{
"method": "send",
"type": "GET",
"class": "com.example.bootkafka.controller.OrderController",
"url": "/send"
}
]