SpringMVC项目中获取所有URL到Controller Method的映射

参考链接 https://www.cnblogs.com/yuananyun/archive/2014/08/25/3934371.html

 

参考上面大神的代码写的,发现他的代码有点小小的问题啊,我这只菜鸟有点小小不懂哈,然后我就Ctrl+C了一下,然后修改一下了啊,改成我寄己可以运行的啊,下面上代码

Controller代码

    @ResponseBody
    @RequestMapping("/url/list/show")
    public String listUrlsShow(){
        Map map =  this.handlerMapping.getHandlerMethods();
        Iterator<?> iterator = map.entrySet().iterator();
        List<RequestToMethodItem> urls=new ArrayList<>();
        while(iterator.hasNext()){
            Map.Entry<RequestMappingInfo, HandlerMethod> entry = (Map.Entry) iterator.next();

            RequestMappingInfo requestMappingInfo = entry.getKey();
            HandlerMethod mappingInfoValue = entry.getValue();
            String controllerName = mappingInfoValue.getBeanType().toString();
            String requestMethodName = mappingInfoValue.getMethod().getName();
            Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes();

            RequestMethodsRequestCondition methodCondition = requestMappingInfo.getMethodsCondition();

            Iterator it=methodCondition.getMethods().iterator();
            String requestType =it.hasNext()?methodCondition.getMethods().iterator().next().name():"";

                    PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
            String requestUrl = patternsCondition.getPatterns().iterator().next();

            RequestToMethodItem item = new RequestToMethodItem(requestUrl,
                    requestType,
                    controllerName,
                    requestMethodName,
                    methodParamTypes);

            urls.add(item);
        }

        return successResponse(urls);
    }

使用到的POJO代码:

RequestToMethodItem.java
public class RequestToMethodItem {
    public String controllerName;
    public String methodName;
    public String requestType;
    public String requestUrl;
    public Class<?>[] methodParmaTypes;

    public RequestToMethodItem(String requestUrl, String requestType, String controllerName, String requestMethodName,
                        Class<?>[] methodParmaTypes) {
        this.requestUrl = requestUrl;
        this.requestType = requestType;
        this.controllerName = controllerName;
        this.methodName = requestMethodName;
        this.methodParmaTypes = methodParmaTypes;
    }


}

好啦好啦,就这些代码啦,撒花~~~

说明一下哈,敲黑板,注意听:

successResponse()方法是我自己封装的哈,功能是把最后得到的结果List,转换成json对象哈,反正最后结果都在urls中啦。可以选择打印撒。。

运行一下哦,come on baby!!!  输入链接,访问访问。。。

下面就是运行结果图啦,散开散开,图有点大鸭

 

转载于:https://www.cnblogs.com/zhang-cb/p/9890900.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值