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:5313 */14 public class Test {15 16 /**17 * 方法描述:main方法测试18 * 作者:赵鹏19 * 时间:2016-10-8 上午09:04:5320 */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 }