java获取所有controller方法_SpringMvc实战--在一个页面显示Controller中所有的方法信息...

本文介绍如何在SpringMVC项目中显示所有使用@RequestMapping注解的方法信息,包括请求URL、方法、参数等。通过Spring的RequestMappingHandlerMapping获取处理映射,解析输出的信息,获取方法参数和注解,并通过LocalVariableTableParameterNameDiscoverer获取参数名。此外,自定义WaltzDocument注解增强接口描述,提高前后端开发效率。
摘要由CSDN通过智能技术生成

0.说明

本博客记录的是如何显示SpringMVC框架中所有使用@RequestMapping注解标注的方法.

由于项目需要,web框架使用SpringMVC.前端\客户端\后端是分开的不同组的人,所以不可避免的要编写\更新大量的接口说明文档.这大大降低了效率,因此实现了显示SpringMVC中所有接受请求的方法的信息的功能.

总体思想:从Spring容器中找到所有加了RequestMapping注解的方法,并且集中显示.

1.用处

显示SpringMVC中所有加了@RequestMapping注解的方法信息

2.实现

2.0.环境说明

使用maven开发,别的不多说了.直接上pom

org.springframework

spring-webmvc

${org.springframework.version}

org.springframework

spring-web

${org.springframework.version}

org.springframework

spring-test

${org.springframework.version}

test

其中

3.2.13.RELEASE

底层的spring core之类的也是3.2.13.RELEASE

2.1.显示所有请求信息

细心的同学可能发现了.在springMVC项目启动的时候,会出现很多的信息.

其中,和咱们这个文章相关的就是这个类输出的信息了:

2015-05-18 12:18:47,253 INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/guide/display/search],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json;charset=UTF-8],custom=[]}" onto public java.lang.String com.renren.toro.waltz.web.controller.guide.DisplayController.search()

2015-05-18 12:18:47,253 INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/guide/display],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.renren.toro.waltz.web.controller.guide.DisplayController.index()

2015-05-18 12:18:47,253 INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/guide/display/detail],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.renren.toro.waltz.web.controller.guide.DisplayController.detail(int)

2015-05-18 12:18:47,255 INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/logout],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json;charset=UTF-8],custom=[]}" onto public java.lang.String com.renren.toro.waltz.web.controller.LogoutController.logout(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

还有很多,只是截取了一段.

这个RequestMappingHandlerMapping 类会输出所有的Map信息.

下面介绍信息中的数据

这里以这一条输出作为实例:

2015-05-18 12:18:47,255 INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/logout],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json;charset=UTF-8],custom=[]}" onto public java.lang.String com.renren.toro.waltz.web.controller.LogoutController.logout(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

序号

名称

说明

1

/logout

对应的接受请求的url

2

methods

接受请求的方法

3

params

请求的参数

4

headers

请求的header信息

5

consumes

接受请求的类型

6

produces

返回的数据类型

后边会输出对应的类和方法信息:

onto public java.lang.String com.renren.toro.waltz.web.controller.LogoutController.logout(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

因此从这里入手.

我这里使用了annotation-driven,所以可以直接

@Autowired

private RequestMappingHandlerMapping requestMappingHandlerMapping;

这样就可以在一个handler方法中获取所有的添加了@RequestMapping的方法了.

具体如下:

1 //request methods

2 Map handlerMethods =requestMappingHandlerMapping.getHandlerMethods();3 Set keySet =handlerMethods.keySet();4

5 for(RequestMappingInfo requestMappingInfo : keySet) {6 //请求路径

7 String path =requestMappingInfo.getPatternsCondition().toString();8

9 //请求方法

10 String requestMethod =requestMappingInfo.getMethodsCondition().toString();11

18 //返回header类型

19 String responseType =requestMappingInfo.getProducesCon

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值