Spring 如何获取所有控制器的所有请求映射?

Spring 如何获取所有控制器的所有请求映射?,spring,spring-mvc,spring-4,Spring,Spring Mvc,Spring 4,我创建了许多控制器,指定了每个控制器的请求映射,以便在应用程序中识别它们: 那么,如何以编程方式获取项目中所有控制器的requestmappings的所有值呢?我想得到一个类似列表的东西,其中包含例如“/”、“/ajax”。您可以通过使用谷歌番石榴和Java反射API来实现这一点 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactI

我创建了许多
控制器
,指定了每个控制器的请求映射,以便在应用程序中识别它们:


那么,如何以编程方式获取项目中所有控制器的requestmappings的所有值呢?我想得到一个类似列表的东西,其中包含例如“/”、“/ajax”。

您可以通过使用谷歌番石榴Java反射API来实现这一点

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>19.0</version>
</dependency>
下面的代码将从单个控制器打印所有@RequestMapping,您可以更新此代码

 Class clazz = Class.forName("com.abc.cbe.rest.controller.MyController"); // or list of controllers, //you can iterate that list
    for(Method method :clazz.getMethods()){
         for(Annotation annotation :method.getDeclaredAnnotations()){
             if(annotation.toString().contains("RequestMapping"))
            System.out.println("Annotation is..."+ annotation.toString());
     }
     }

getClassOfPackage
方法不起作用:控制台中没有输出!我已经在pom.xml中添加了依赖项并更新了项目。我在这里共享之前测试了该代码,您调试了吗?我应该调试哪些行?哈哈哈,我不是那个家伙;-)关于
类的上一个代码。forName
不会给出控制器的结果,它只输出控制器内部的操作!
public void getClassOfPackage(String packagenom) {

    final ClassLoader loader = Thread.currentThread()
            .getContextClassLoader();
    try {

        ClassPath classpath = ClassPath.from(loader); // scans the class path used by classloader
        for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClasses(packagenom)) {
         if(!classInfo.getSimpleName().endsWith("_")){
            System.out.println(classInfo.getSimpleName());
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 Class clazz = Class.forName("com.abc.cbe.rest.controller.MyController"); // or list of controllers, //you can iterate that list
    for(Method method :clazz.getMethods()){
         for(Annotation annotation :method.getDeclaredAnnotations()){
             if(annotation.toString().contains("RequestMapping"))
            System.out.println("Annotation is..."+ annotation.toString());
     }
     }