14

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. Currently I do it like this:

for (final Object bean: this.context.getBeansWithAnnotation(
        Controller.class).values())
{
    ...Generate controller documentation for the bean...
}

But the first call of this code is EXTREMELY slow. I wonder if Spring iterates over ALL classes in the classpath instead of just checking the defined beans. The controllers are already loaded when the above code is run, the log displays all of them with their request mappings so Spring MVC must already know them all and there must be a faster way to get a list of them. But how?

3
  • I wonder why would you need that info, since you're doing the annotation of @Controller (s) anyway
    – ant
    Jun 5, 2012 at 13:21
  • 3
    He has mentioned that in the question very clearly that he wants to generate the documentation for those controllers. Jun 5, 2012 at 13:25
  • The answer I provided here can achieve this as well. Nov 28, 2020 at 1:48

2 Answers 2

37

I like the approach suggested by @Japs, but would also like to recommend an alternate approach. This is based on your observation that the classpath has already been scanned by Spring, and the controllers and the request mapped methods configured, this mapping is maintained in a handlerMapping component. If you are using Spring 3.1 this handlerMapping component is an instance of RequestMappingHandlerMapping, which you can query to find the handlerMappedMethods and the associated controllers, along these lines(if you are on an older version of Spring, you should be able to use a similar approach):

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Controller
public class EndpointDocController {
 private final RequestMappingHandlerMapping handlerMapping;
 
 @Autowired
 public EndpointDocController(RequestMappingHandlerMapping handlerMapping) {
  this.handlerMapping = handlerMapping;
 }
  
 @RequestMapping(value="/endpointdoc", method=RequestMethod.GET)
 public void show(Model model) {
  model.addAttribute("handlerMethods", this.handlerMapping.getHandlerMethods());
 } 
}

I have provided more details on this at this url http://biju-allandsundry.blogspot.com/2012/03/endpoint-documentation-controller-for.html

This is based on a presentation on Spring 3.1 by Rossen Stoyanchev of Spring Source.

2
  • 1
    this is pretty awesome. I'd go with this one Jun 5, 2012 at 15:34
  • Using/defining @Autowired private RequestMappingHandlerMapping handlerMapping; In class also works instead of defining final and having to pass it as argument and initialize it in constructor, Later on if you use a custom user made object (obviously not a controller) to initialize. It can be a problem
    – Merv
    Apr 4, 2018 at 15:04
17

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.

3
  • Good snippet (+1). But I think that it is too low level. I mean this one scanns classes. I believe that getBeansWithAnnotation() implementation should use scanner inside.
    – AlexR
    Jun 5, 2012 at 13:26
  • May be you are right. But he wants a faster way to achieve this. Ans I have used the above code snippet and its not slow for me. Thats why I suggested this. And this scanner class is provided by Spring itself so according to me its not low level. Jun 5, 2012 at 13:27
  • 1
    Works great and MUCH faster than getBeansWithAnnotation(). Thanks!
    – kayahr
    Jun 5, 2012 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.