SpringMvc获取控制器所有的URL路径
@Autowired
WebApplicationContext context;
@Test
void getControllerPath() {
RequestMappingHandlerMapping mapping = context.getBean(RequestMappingHandlerMapping.class);
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
List<Map<String, String>> listMap = new ArrayList<>();
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : map.entrySet()) {
Map<String, String> initMap = new HashMap<>();
RequestMappingInfo info = entry.getKey();
RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
PatternsRequestCondition pattern = info.getPatternsCondition();
Set<RequestMethod> methods = methodsCondition.getMethods();
if (!ObjectUtils.isEmpty(pattern) && !CollectionUtils.isEmpty(methods)) {
if (!CollectionUtils.isEmpty(pattern.getPatterns())) {
Set<String> patterns = pattern.getPatterns();
log.info("url:{}", patterns.toString());
for (String url : patterns) {
initMap.put("url", url);
initMap.put("name", url.replaceAll("/", "_").substring(1));
}
for (RequestMethod requestMethod : methods) {
initMap.put("type", requestMethod.toString());
}
}
listMap.add(initMap);
}
}
}