Spring是一个很好很强大的开源框架,它就像是一个容器,为我们提供了各种Bean组件和服务。对于MVC这部分而言,它里面实现了从Url请求映射控制器方法的逻辑处理,在我们平时的开发工作中并不需要太多的理会这个Url是怎么和控制器中的方法建立映射的,一切都由Spring MVC帮我们搞定了。

但是我今天在做SDK工程的时候,突然产生一个想法:能否把我项目中的所有Url和Method的映射信息打印出来?以便我一眼就看出我已经完成了那些API接口开发,这些方法需要什么参数。就像下图所示:

有了想法就要用行动,第一步肯定是要去看看别人是否已经解决了这个问题啦。查了半天的资料,倒是发现有几个相似的问题,但都没有满意的答案,只好自己调试Spring,跟踪它的处理步骤,终于让我发现了一个可行的办法,不多说,直接贴代码:

1、首先建立一个类来保存我想要的东东

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、然后就是收集信息创建对象啦

@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); //请求url和处理方法的映射
List<RequestToMethodItem> requestToMethodItemList = new ArrayList<RequestToMethodItem>();
//获取所有的RequestMapping
Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext,
HandlerMapping.class, true, false);

        for (HandlerMapping handlerMapping : allRequestMappings.values())
{
//本项目只需要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”中的映射信息,其实还有另外三个HandlerMapping,如果想要获取项目中所有的映射信息,肯定是一个都不能放过了。调试的 信息如下:

3、取到数据后就展示呗,借助Thymeleaf,很容易就实现了展示,效果就是第一张图。

<!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>请求方法列表</title>
</head> <body>
<div style="margin: 0;padding: 0;text-align: center"><h1>请求方法列表</h1></div>
<div>
<ul>
<li th:each="method:${MethodList}">
<h3 th:text="${method.methodName}"></h3> <p th:text="'所属控制器:'+${method.controllerName}"></p> <p th:text="'请求URL:'+${method.requestUrl}"></p> <p th:text="'请求方式:'+${method.requestType}"></p> <div>
<p>方法参数列表:</p>
<ul>
<li th:each="param:${method.methodParmaTypes}">
<p th:text="${param}"></p>
</li>
</ul>
</div> </li>
</ul>
</div> </body>
</html>

SpringMVC项目中获取所有URL到Controller Method的映射的更多相关文章

  1. 在ASP.NET MVC 中获取当前URL、controller、action

    一.URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟 ...

  2. 在ASP.NET MVC 中获取当前URL、controller、action(转)

    URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟目录 ...

  3. 如何在ASP.NET MVC 中获取当前URL、controller、action

    一.URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟 ...

  4. 在ASP.NET MVC 中获取当前URL、controller、action 、参数

    URL的获取很简单,ASP.NET通用:[1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟目录名 ...

  5. nginx的rewrite ,如何在flask项目中获取重写前的url

    1. 在flask配一个重写到哪的路由,假设是/rewite/,然后到nginx的配置文件写重写规则,我这里重写全部的请求,接着测试能否重写成功 1. 添加一个路由 配置重写规则 测试成功 2.接下来 ...

  6. Django自动获取项目中的全部URL

    import re from collections import OrderedDict from django.conf import settings from django.utils.mod ...

  7. ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action (转载)

    ASP.NET MVC 一.获取URL(ASP.NET通用): [1]获取完整url(协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [ ...

  8. 无法从项目中获取SSIS包的列表

    一直做的SSIS项目,突然在生成项目的时候没有反应,crtl + alt +o 提示:无法从项目中获取SSIS包的列表,发现是最近的包没有设计数据源, 解决思路:检查最近的包,挨个运行一遍,看看有没有 ...

  9. SpringMVC项目中web.xml中的节点载入顺序问题

    SpringMVC项目中web.xml中的节点载入顺序问题,之前以为web.xml中就是一些配置信息,和节点的顺序没有关系.后来才发现初始化时的载入顺序是和节点的顺序相关的. 完整的web.xml文件 ...

随机推荐

  1. 运行Delphi XE10的MongoDB例程,测试Delphi插入记录性能

    Delphi XE10支持MongoDB的数据库,提供了个例子restaurants可批量导入数据. 本文对比Delphi例子与MongoDB自带的mongoimport导入批量数据的性能. 步骤: ...

  2. Java HashMap 源代码分析

    Java HashMap jdk 1.8 Java8相对于java7来说HashMap变化比较大,在hash冲突严重的时候java7会退化为链表,Java8会退化为TreeMap 我们先来看一下类图: ...

  3. [Golang学习笔记] 05 程序实体2 作用域访问权限和变量重声明

    作用域访问权限: 程序实体访问权限(作用域)有三种:1. 包级私有(代码包)2. 模块级私有(代码包)3. 公开(全域). 一个函数是一个代码块.一个程序实体的作用域总是会被限制在某个代码块中.好处: ...

  4. Go语言中的字符串处理

    1 概述 字符串,string,一串固定长度的字符连接起来的字符集合.Go语言的字符串是使用UTF-8编码的.UTF-8是Unicode的实现方式之一. Go语言原生支持字符串.使用双引号(“”)或反 ...

  5. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

  6. 20155319 2016-2017-2《Java程序设计》课程总结

    20155319 2016-2017-2<Java程序设计>课程总结 每周作业链接 预备作业1:亦师亦友--我所期望的师生关系,对专业的认识与期望等 预备作业2:没有了自主,学习的小船说翻 ...

  7. 20155322 2016-2017-2 《Java程序设计》 第一周学习总结

    20155322 2016-2017-2 <Java程序设计> 第一周学习总结 教材学习内容总结 本周学习内容的主要是: 一.浏览教材,根据自己的理解每章提出一个问题. 在浏览教材后,我提 ...

  8. 多级反馈序列c语言模拟实现

    多级反馈队列调度算法: 1.设置多个就绪队列,并给队列赋予不同的优先级数,第一个最高,依次递减. 2.赋予各个队列中进程执行时间片的大小,优先级越高的队列,时间片越小. 3.当一个新进程进入内存后,首 ...

  9. (ex)Lucas总结

    (ex)Lucas总结 普通Lucas 求 \[ C_n^m\;mod\;p \] 其中\(n,m,p\leq 10^5\)其中\(p\)为质数 公式不难背,那就直接背吧... \[ C_n^m\;m ...

  10. WPF DataGridTable

    由于项目要显示表头合并,而数据源列随时变更,又不想重复的画表格,就实现动态数据(dynamic)绑定和配置数据列模板的方式 编辑DataGridColumnHeader样式实现表头合并:效果如下 实现 ...