SpringMVC项目中获取所有URL到Controller Method的映射
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的映射的更多相关文章
- 在ASP.NET MVC 中获取当前URL、controller、action
一.URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟 ...
- 在ASP.NET MVC 中获取当前URL、controller、action(转)
URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟目录 ...
- 如何在ASP.NET MVC 中获取当前URL、controller、action
一.URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟 ...
- 在ASP.NET MVC 中获取当前URL、controller、action 、参数
URL的获取很简单,ASP.NET通用:[1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟目录名 ...
- nginx的rewrite ,如何在flask项目中获取重写前的url
1. 在flask配一个重写到哪的路由,假设是/rewite/,然后到nginx的配置文件写重写规则,我这里重写全部的请求,接着测试能否重写成功 1. 添加一个路由 配置重写规则 测试成功 2.接下来 ...
- Django自动获取项目中的全部URL
import re from collections import OrderedDict from django.conf import settings from django.utils.mod ...
- ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action (转载)
ASP.NET MVC 一.获取URL(ASP.NET通用): [1]获取完整url(协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [ ...
- 无法从项目中获取SSIS包的列表
一直做的SSIS项目,突然在生成项目的时候没有反应,crtl + alt +o 提示:无法从项目中获取SSIS包的列表,发现是最近的包没有设计数据源, 解决思路:检查最近的包,挨个运行一遍,看看有没有 ...
- SpringMVC项目中web.xml中的节点载入顺序问题
SpringMVC项目中web.xml中的节点载入顺序问题,之前以为web.xml中就是一些配置信息,和节点的顺序没有关系.后来才发现初始化时的载入顺序是和节点的顺序相关的. 完整的web.xml文件 ...
随机推荐
- Delphi XE5的Android开发平台搭建
Delphi XE5支持Android ARM的开发,可以在Android虚拟机里运行,因此建议将XE5安装在64bit的Windows,内存可以大于3GB Delphi XE5安装光盘中包含了最基本 ...
- Python学习 :反射 & 单例模式
反射 什么是反射? - 反射主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省) 面向对象中的反射 - 通过字符串的形式来操作(获取.检查.增加.删除)对象中的成员 - python中的 ...
- css中元素的分类
按照显示元素分类: 行内元素(lnline-element):元素的高度,行高,顶底边距由元素所包含的图片或文字所决定,不可改变:其宽度为内容文字或图片的宽度所决定,而其左右边距可人为设置. 行内元素 ...
- 与虚拟机和linux的初次接触
初次接触虚拟机 根据老师所给的资源和教程,虚拟机安装的过程十分顺利. 接下来是在虚拟机上安装linux操作系统我下载了破解版的Ubuntu,也是十分顺利 接下来就是安装虚拟机增强功能,命令有些繁琐,在 ...
- 15、Java并发编程:Callable、Future和FutureTask
Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...
- Drupal7重置密码方法
Drupal版本 7.40 方法1: 根目录index.php添加 require_once 'includes/password.inc'; require_once 'includes/boots ...
- 质造未来,首届腾讯WeTest技术交流开放日成功举办
WeTest 导读 北京时间12月21日下午1点整,2018年度腾讯WeTest技术交流开放日在上海举办.盛大.巨人.电魂.bilibili.方趣等十余家来自不同优秀企业的测试技术负责人均来到现场,共 ...
- 优化 VR 动作类游戏《Space Pirate Trainer*》以便在英特尔® 集成显卡上实现卓越的表现
Space Pirate Trainer* 是一款面向 HTC Vive*.Oculus Touch* 和 Windows Mixed Reality* 的原创发行游戏.版本 1.0 于 2017 年 ...
- Unity FSM 有限状态机
翻译了一下unity wiki上对于有限状态机的案例,等有空时在详细写一下.在场景中添加两个游戏物体,一个为玩家并修改其Tag为Player,另一个为NPC为其添加NPCControl脚本,并为其将玩 ...
- 如何使用Win+R快捷键打开自定义程序
鉴于大家对于提高效率这块有争议,更改了下标题. 大家平时一定都使用过Win+R运行快捷键, 在运行里可以快捷的打开一些系统软件,比如说输入mstsc是打开远程连接,输入explorer是打开文件管理器 ...

