国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

SpringMVC項(xiàng)目中獲取所有URL到Controller Method的映射

2019-11-14 20:59:33
字體:
供稿:網(wǎng)友
SPRingMVC項(xiàng)目中獲取所有URL到Controller Method的映射

Spring是一個(gè)很好很強(qiáng)大的開源框架,它就像是一個(gè)容器,為我們提供了各種Bean組件和服務(wù)。對(duì)于MVC這部分而言,它里面實(shí)現(xiàn)了從Url請(qǐng)求映射控制器方法的邏輯處理,在我們平時(shí)的開發(fā)工作中并不需要太多的理會(huì)這個(gè)Url是怎么和控制器中的方法建立映射的,一切都由Spring MVC幫我們搞定了。

但是我今天在做SDK工程的時(shí)候,突然產(chǎn)生一個(gè)想法:能否把我項(xiàng)目中的所有Url和Method的映射信息打印出來?以便我一眼就看出我已經(jīng)完成了那些API接口開發(fā),這些方法需要什么參數(shù)。就像下圖所示:

image_thumb5

有了想法就要用行動(dòng),第一步肯定是要去看看別人是否已經(jīng)解決了這個(gè)問題啦。查了半天的資料,倒是發(fā)現(xiàn)有幾個(gè)相似的問題,但都沒有滿意的答案,只好自己調(diào)試Spring,跟蹤它的處理步驟,終于讓我發(fā)現(xiàn)了一個(gè)可行的辦法,不多說,直接貼代碼:

1、首先建立一個(gè)類來保存我想要的東東

private 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;        }    }

2、然后就是收集信息創(chuàng)建對(duì)象啦

@RequestMapping(value = "/index", method = RequestMethod.GET)    public ModelAndView index(HttpServletRequest request)    {        ServletContext servletContext = request.getsession().getServletContext();        if (servletContext == null)        {            return null;        }        WebapplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);        //請(qǐng)求url和處理方法的映射        List<RequestToMethodItem> requestToMethodItemList = new ArrayList<RequestToMethodItem>();        //獲取所有的RequestMapping        Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, 
HandlerMapping.class, true, false);        for (HandlerMapping handlerMapping : allRequestMappings.values())        {            //本項(xiàng)目只需要RequestMappingHandlerMapping中的URL映射            if (handlerMapping instanceof RequestMappingHandlerMapping)            {                RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerMapping;                Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();                for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet())                {                    RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey();                    HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry.getValue();                    RequestMethodsRequestCondition methodCondition = requestMappingInfo.getMethodsCondition();                    String requestType = SetUtils.first(methodCondition.getMethods()).name();                    PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();                    String requestUrl = SetUtils.first(patternsCondition.getPatterns());                    String controllerName = mappingInfoValue.getBeanType().toString();                    String requestMethodName = mappingInfoValue.getMethod().getName();                    Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes();                    RequestToMethodItem item = new RequestToMethodItem(requestUrl, requestType, controllerName, requestMethodName, 
methodParamTypes);                    requestToMethodItemList.add(item);                }                break;            }        }        return new ModelAndView("index").addObject("MethodList", requestToMethodItemList);    }

這一步我需要說明一下,我這里只用了“RequestMappingHandlerMapping”中的映射信息,其實(shí)還有另外三個(gè)HandlerMapping,如果想要獲取項(xiàng)目中所有的映射信息,肯定是一個(gè)都不能放過了。調(diào)試的 信息如下:

image_thumb7

3、取到數(shù)據(jù)后就展示唄,借助Thymeleaf,很容易就實(shí)現(xiàn)了展示,效果就是第一張圖。

<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head>    <title>Spring Thyme Seed Starter Manager</title>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>    <link rel="stylesheet" type="text/CSS" media="all" href="../../css/stsm.css" th:href="@{/css/stsm.css}"/>    <title>請(qǐng)求方法列表</title></head><body><div style="margin: 0;padding: 0;text-align: center"><h1>請(qǐng)求方法列表</h1></div><div>    <ul>        <li th:each="method:${MethodList}">            <h3 th:text="${method.methodName}"></h3>            <p th:text="'所屬控制器:'+${method.controllerName}"></p>            <p th:text="'請(qǐng)求URL:'+${method.requestUrl}"></p>            <p th:text="'請(qǐng)求方式:'+${method.requestType}"></p>            <div>                <p>方法參數(shù)列表:</p>                <ul>                    <li th:each="param:${method.methodParmaTypes}">                        <p th:text="${param}"></p>                    </li>                </ul>            </div>        </li>    </ul></div></body></html>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 扎鲁特旗| 盘锦市| 华蓥市| 绥宁县| 吉木乃县| 高邑县| 宝丰县| 贺兰县| 闻喜县| 垦利县| 新和县| 江源县| 拉萨市| 磐石市| 金沙县| 塘沽区| 虹口区| 翁牛特旗| 玉龙| 信阳市| 类乌齐县| 太保市| 海淀区| 纳雍县| 双桥区| 汶上县| 资溪县| 永城市| 无极县| 呼玛县| 彭山县| 麻栗坡县| 禹城市| 浦北县| 民勤县| 渭源县| 天柱县| 嵊泗县| 财经| 大丰市| 武强县|