首页 > 移动平台 > 详细

通过反射获得 spring 的 RequestMapping value值

时间:2016-12-08 11:42:54      阅读:353      评论:0      收藏:0      [点我收藏+]

package demo

import java.lang.reflect.Method;

import org.springframework.web.bind.annotation.RequestMapping;

import com.demo.controller.TicketController;

/**
 * 文档描述:通过反射得到requestMapping的value值
 * 作者:赵鹏
 * 时间:2016-10-8 上午09:04:53
 */
public class Test {

    /**
     * 方法描述:main方法测试
     * 作者:赵鹏
     * 时间:2016-10-8 上午09:04:53
     */
    public static void main(String[] args) {
        
        //得到字节码文件  【只需要更改controller类名】
        Class<?> clazz = TicketController.class;
        
        //得到方法
        Method[] methods = clazz.getDeclaredMethods();
        
        for (Method method : methods) {
            
            //判断是否存在requestMapping注释
            boolean present = method.isAnnotationPresent(RequestMapping.class);
            
            if(present){
                
                //得到requestMapping注释
                RequestMapping annotation = method.getAnnotation(RequestMapping.class);
                
                //输出 annotation RequestMapping包含的信息(headers=[], name=, path=[], value=[toTicket], produces=[], method=[], params=[], consumes=[])
                //System.err.println(annotation);
                
                //得到value数组
                String[] value = annotation.value();
                
                for (String string2 : value) {
                    
                    //输出value值
                    System.out.println(string2);
                    
                }
                
            }
            
        }

    }

}

通过反射获得 spring 的 RequestMapping value值

原文:http://www.cnblogs.com/zhao-blog/p/6144240.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!