首页
学习
活动
专区
圈层
工具
发布

SpringFramework之获取controller所有的url

    首先要获取WebApplicationContext,怎么获取见我的这篇博客

    拿到WebApplicatioContext之后,就可以了,如下List-1所示,用单元测试就可以得到我们需要的url了,直接上代码了。

List-1

代码语言:javascript
复制
@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);
    });
}
代码语言:txt
复制
 (adsbygoogle = window.adsbygoogle || []).push({});
下一篇
举报
领券