SpringFramework之獲取controller全部的url

    首先要獲取WebApplicationContext,怎麼獲取見個人這篇博客java

    拿到WebApplicatioContext以後,就能夠了,以下List-1所示,用單元測試就能夠獲得咱們須要的url了,直接上代碼了。web

    List-1app

@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);
    });
}
相關文章
相關標籤/搜索