Springboot中怎么获取所有URL请求及其请求方式
写在前面
这个作用,除了可以统计,项目开发中所有的接口路径,可以对比接口设计文档中的改动,提供两种方案
一、Swagger实现
Swagger,集成统计、测试所有的接口,需要相关jar和配置,这里不介绍太多,自主学习
二、使用Spring对象获取,WebApplicationContext
简单统计实现
@Autowired
WebApplicationContext applicationContext;
@GetMapping("/getAllUrl")
public List<String> getAllUrl(){
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
//获取url与类和方法的对应信息
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
List<String> urls = new ArrayList<>();
for (RequestMappingInfo info : map.keySet()){
//获取url的Set集合,一个方法可能对应多个url
Set<String> patterns = info.getPatternsCondition().getPatterns();
// 这里可获取请求方式 Get,Post等等
// Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
for (String url : patterns){
urls.add(url);
}
}
return urls;
}
获取结果
[
"/pis/absence/template-download",
"/pis/absence/add",
...,
...,
...
]
这里用到Spring web架构中的一个顶级对象 WebApplicationContext
public interface WebApplicationContext extends ApplicationContext {
/** * Context attribute to bind root WebApplicationContext to on successful startup. * <p>Note: If the startup of the root context fails, this attribute can contain * an exception or error as value. Use WebApplicationContextUtils for convenient * lookup of the root WebApplicationContext. * @see org.springframework.web.context.support.WebApplicationContextUtils#getWebApplicationContext * @see org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext */
String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
/** * Scope identifier for request scope: "request". * Supported in addition to the standard scopes "singleton" and "prototype". */
String SCOPE_REQUEST = "request";
/** * Scope identifier for session scope: "session". * Supported in addition to the standard scopes "singleton" and "prototype". */
String SCOPE_SESSION = "session";
/** * Scope identifier for the global web application scope: "application". * Supported in addition to the standard scopes "singleton" and "prototype". */
String SCOPE_APPLICATION = "application";
/** * Name of the ServletContext environment bean in the factory. * @see javax.servlet.ServletContext */
String SERVLET_CONTEXT_BEAN_NAME = "servletContext";
/** * Name of the ServletContext/PortletContext init-params environment bean in the factory. * <p>Note: Possibly merged with ServletConfig/PortletConfig parameters. * ServletConfig parameters override ServletContext parameters of the same name. * @see javax.servlet.ServletContext#getInitParameterNames() * @see javax.servlet.ServletContext#getInitParameter(String) * @see javax.servlet.ServletConfig#getInitParameterNames() * @see javax.servlet.ServletConfig#getInitParameter(String) */
String CONTEXT_PARAMETERS_BEAN_NAME = "contextParameters";
/** * Name of the ServletContext/PortletContext attributes environment bean in the factory. * @see javax.servlet.ServletContext#getAttributeNames() * @see javax.servlet.ServletContext#getAttribute(String) */
String CONTEXT_ATTRIBUTES_BEAN_NAME = "contextAttributes";
/** * Return the standard Servlet API ServletContext for this application. */
@Nullable
ServletContext getServletContext();
}
源码太过抽象,下图是 debug 测试中,看到的加载数据,从他这里可以获取springMvc中所有的对象,包括 Bean加载,生命周期,webServer等等…