Spring MVC获取所有注册的url

来源:互联网 发布:mac u盘无法分区 编辑:程序博客网 时间:2024/06/05 19:59

本文的目的是获取所有通过requestMapping注册的url


    @RequestMapping("getAllUrl")                                       @ResponseBody    public Set<String> getAllUrl(HttpServletRequest request) {        Set<String> result = new HashSet<String>();        WebApplicationContext wc = (WebApplicationContext) request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);        RequestMappingHandlerMapping bean = wc.getBean(RequestMappingHandlerMapping.class);        Map<RequestMappingInfo, HandlerMethod> handlerMethods = bean.getHandlerMethods();        for (RequestMappingInfo rmi : handlerMethods.keySet()) {            PatternsRequestCondition pc = rmi.getPatternsCondition();            Set<String> pSet = pc.getPatterns();            result.addAll(pSet);        }        return result;    }

通过上面的代码就可以获取所有注册到容器中的url。

0 0