zoukankan      html  css  js  c++  java
  • 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!!!  输入链接,访问访问。。。

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

  • 相关阅读:
    Niagara技术文档汇总
    cPickle对python对象进行序列化,序列化到文件或内存
    html = data.decode('gbk').encode('utf-8')
    用200行Python代码“换脸”
    No module named cv2
    JAVA Calendar具体解释
    千万别用模板给的list.size()巨坑
    div仿checkbox表单样式美化及功能
    动态规划0—1背包问题
    拒绝乱码:做外贸系统,打印俄文快递单时,避免俄文乱码的方法
  • 原文地址:https://www.cnblogs.com/zhang-cb/p/9890900.html
Copyright © 2011-2022 走看看