spring mvc 获取所有的controller中的url

spring mvc 获取所有的controller中的url

原理:


从WebApplicationContext中获取RequestMappingHandlerMapping.class类型的所有bean.


代码如下:


在web.xml中增加:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>

    <listener>
        <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

新增ControllerReflect

@Controller
@RequestMapping("/controllerReflect")
public class ControllerReflect {
    @ResponseBody
    @RequestMapping("getUrlMapping")
    public Object getUrlMapping(HttpServletRequest request) {
        WebApplicationContext wc = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
        RequestMappingHandlerMapping rmhp = wc.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> map = rmhp.getHandlerMethods();
        for(RequestMappingInfo info : map.keySet()){
            System.out.println(info.getPatternsCondition().toString()
                    + ","
                    +map.get(info).getBean().toString());
        }
        return null;
    }
}


阅读更多
文章标签: spring spring mvc java
个人分类: J2EE
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭