首先要获取WebApplicationContext,怎么获取见我的这篇博客。
拿到WebApplicatioContext之后,就可以了,如下List-1所示,用单元测试就可以得到我们需要的url了,直接上代码了。
List-1
@Test
public void test2() {
Set<String> result = new TreeSet<>();
RequestMappingHandlerMapping bean = webApplicationContext.getBean(RequestMappingHandlerMapping.class);
Map<RequestMappingInfo, HandlerMethod> handlerMethods = bean.getHandlerMethods();
for (RequestMappingInfo rmi : handlerMethods.keySet()) {
PatternsRequestCondition pc = rmi.getPatternsCondition();
Set<String> pSet = pc.getPatterns();
pSet.forEach(url -> {
if (result.contains(url)) {
System.out.println(url);
throw new RuntimeException("url重复");
}
});
result.addAll(pSet);
}
result.forEach(url -> {
System.out.println(url);
});
}
(adsbygoogle = window.adsbygoogle || []).push({});