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

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

  • 相关阅读:
    maven继承父工程统一版本号
    shiro权限控制参考
    动态查询列表页面的分页
    SVN服务器更改ip地址后怎么办
    cookie记住密码功能
    分享小插件的问题
    阿里云短信验证
    从svn上更新maven项目时,所有文件变成包的形式
    Maven工具
    Mybatis的dao层传递单参出现的问题
  • 原文地址:https://www.cnblogs.com/zhang-cb/p/9890900.html
Copyright © 2020-2023  润新知