获取springmvc中所有的Controller

@RequestMapping(value = "getUrlMapping") public ModelAndView  getUrlMapping(HttpServletRequest request) {
    WebApplicationContext wc = getWebApplicationContext(request.getSession().getServletContext());
    RequestMappingHandlerMapping rmhp = (RequestMappingHandlerMapping)wc.getBean("org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0");
    Map<RequestMappingInfo, HandlerMethod> map = rmhp.getHandlerMethods(); for (Iterator<RequestMappingInfo> iterator = map.keySet().iterator(); iterator
            .hasNext();) {
        RequestMappingInfo info = iterator.next();
        System.out.print(info.getConsumesCondition());
        System.out.print(info.getCustomCondition());
        System.out.print(info.getHeadersCondition());
        System.out.print(info.getMethodsCondition());
        System.out.print(info.getParamsCondition());
        System.out.print(info.getPatternsCondition());
        System.out.print(info.getProducesCondition());

        System.out.print("===");

        HandlerMethod method = map.get(info);
        System.out.print(method.getMethod().getName() + "--");
        System.out.print(method.getMethodAnnotation(RequestMapping.class).params()[0]);
        System.out.println();
    } return null;
} public WebApplicationContext getWebApplicationContext(ServletContext sc) { return WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
}



代码如上:

需要注意的是


RequestMappingHandlerMapping rmhp = (RequestMappingHandlerMapping)wc.getBean("org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0");

取RequestMappingHandlerMapping 的时候,如果集成了多个展示方式的话,RequestMappingHandlerMapping 对象会有多个,直接通过class是娶不到的。但是可以通过上面的方式获取到。



你可能感兴趣的:(获取springmvc中所有的Controller)