Springboot中怎么获取所有URL请求及其请求方式

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等等…

全部评论

相关推荐

2024-12-29 11:08
湖南工业大学 Java
程序员牛肉:简历没什么大问题了。 而且不要再换项目了。三月份就开暑期实习了,现在都一月份了。实在来不及重新开一下项目了。把一个项目写完或许很快,但是把一个项目搞懂吃透并不简单。所以不要换项目了,把你简历上面的两个项目好好挖一挖吧。 具体 体现在:你能不能流利的说出你的项目的每一个功能点代码实现?你能不能说出在这块除了A技术之外,还有其他技术能够实现嘛?如果有其他技术能够实现,那你这块为什么选择了你当前用的这个技术?
投递牛客等公司
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务