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!!!  输入链接,访问访问。。。

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

  • 相关阅读:
    电商平台开发笔记5.nuxt项目中深度选择器解决el-input高度设置无效
    电商平台开发笔记4.css选择器之~波浪号使用
    电商平台开发笔记3.nuxt全局css的引入
    电商平台开发笔记2.Nuxt增加对less支持,解决This relative module was not found报错
    电商平台开发笔记1.Nuxt项目创建+Eslint代码保存自动格式化
    vue-cli 4.x 发布前的一些优化
    VueCli 4.x npm run build后主页空白的原因及解决方案
    VSCode 保存时自动ESlint格式化
    git 常用操作笔记
    VSCode下手动构建webpack项目
  • 原文地址:https://www.cnblogs.com/zhang-cb/p/9890900.html
Copyright © 2011-2022 走看看