通过反射获得 spring 的 RequestMapping value值

 1 package demo
 2 
 3 import java.lang.reflect.Method;
 4 
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 
 7 import com.demo.controller.TicketController;
 8 
 9 /**
10  * 文档描述:通过反射得到requestMapping的value值
11  * 作者:赵鹏
12  * 时间:2016-10-8 上午09:04:53
13  */
14 public class Test {
15 
16     /**
17      * 方法描述:main方法测试
18      * 作者:赵鹏
19      * 时间:2016-10-8 上午09:04:53
20      */
21     public static void main(String[] args) {
22         
23         //得到字节码文件  【只需要更改controller类名】
24         Class<?> clazz = TicketController.class;
25         
26         //得到方法
27         Method[] methods = clazz.getDeclaredMethods();
28         
29         for (Method method : methods) {
30             
31             //判断是否存在requestMapping注释
32             boolean present = method.isAnnotationPresent(RequestMapping.class);
33             
34             if(present){
35                 
36                 //得到requestMapping注释
37                 RequestMapping annotation = method.getAnnotation(RequestMapping.class);
38                 
39                 //输出 annotation RequestMapping包含的信息(headers=[], name=, path=[], value=[toTicket], produces=[], method=[], params=[], consumes=[])
40                 //System.err.println(annotation);
41                 
42                 //得到value数组
43                 String[] value = annotation.value();
44                 
45                 for (String string2 : value) {
46                     
47                     //输出value值
48                     System.out.println(string2);
49                     
50                 }
51                 
52             }
53             
54         }
55 
56     }
57 
58 }
原文地址:https://www.cnblogs.com/zhao-blog/p/6144240.html