通過反射獲得 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM