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 }