且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

spring mvc 获取所有请求映射

更新时间:2022-06-02 10:05:09

我正在复制我的一个 以前的答案在这里:

I am replicating one of my previous answers here:

如果你使用的是 Spring 3.1,这个 handlerMapping 组件是 RequestMappingHandlerMapping 的一个实例,你可以查询它以找到 handlerMappedMethods 和相关的控制器,沿着这些行(如果你使用的是旧版本的 Spring,你应该能够使用类似的方法):

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());
 } 
}

我在这个 url http://biju-allandsundry.blogspot.com/2012/03/endpoint-documentation-controller-for.html

本文基于 Spring Source 的 Rossen Stoyanchev 关于 Spring 3.1 的演示.

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