spring mvc get all request mappings

前端 未结 1 881
深忆病人
深忆病人 2020-11-29 23:38

We have a quite big spring mvc web application where the controllers are annotated with @Controller and the methods with @RequestMapping.

I would like to create a te

相关标签:
1条回答
  • 2020-11-30 00:40

    I am replicating one of my previous answers here:

    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.

    0 讨论(0)
提交回复
热议问题