How to find all controllers in Spring MVC?

后端 未结 2 710
-上瘾入骨i
-上瘾入骨i 2020-11-28 07:01

To provide some runtime generated API documentation I want to iterate over all Spring MVC controllers. All controllers are annotated with the Spring @Controller annotation.

2条回答
  •  旧时难觅i
    2020-11-28 07:43

    I have also came across such requirement before some months and I have achieved it using the following code snippet.

    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
            scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class));
            for (BeanDefinition beanDefinition : scanner.findCandidateComponents("com.xxx.yyy.controllers")){
                System.out.println(beanDefinition.getBeanClassName());
            }
    

    You can also do something like this with your controllers.

    Updated the code snippet. Removed the not necessary code and just displaying the class name of the controllers for better understanding. Hope this helps you. Cheers.

提交回复
热议问题