iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Springboot项目如何获取所有的接口
  • 237
分享到

Springboot项目如何获取所有的接口

2024-04-02 19:04:59 237人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录SpringBoot项目获取所有接口获取项目下所有Http接口的信息一、接口信息类二、单元测试springboot项目获取所有接口 @Autowired private WE

springboot项目获取所有接口


@Autowired
private WEBApplicationContext applicationContext; 
@Override
public List getAllUrl() {
    RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
    // 获取url与类和方法的对应信息
    Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
 
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
        Map<String, String> map1 = new HashMap<String, String>();
        RequestMappingInfo info = m.geTKEy();
        HandlerMethod method = m.getValue();
        //获取当前方法所在类名
        Class<?> bean = method.getBeanType();
        //使用反射获取当前类注解内容
        api api = bean.getAnnotation(Api.class);
        RequestMapping requestMapping = bean.getAnnotation(RequestMapping.class);
  String[] value = requestMapping.value();
  map1.put("parent",value[0])
        //获取方法上注解以及注解值
        ApiOperation methodAnnotation = method.getMethodAnnotation(ApiOperation.class);
        String privilegeName = methodAnnotation.notes();
        PatternsRequestCondition p = info.getPatternsCondition();
        for (String url : p.getPatterns()) {
            map1.put("url", url);
        }
        map1.put("className", method.getMethod().getDeclarinGClass().getName()); // 类名
        map1.put("method", method.getMethod().getName()); // 方法名
        RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
        for (RequestMethod requestMethod : methodsCondition.getMethods()) {
            map1.put("type", requestMethod.toString());
        }
        
        list.add(map1);
    } 
   return list;
}

获取项目下所有http接口的信息

一、接口信息类

新建一个类用于存放http接口的相关信息


class RequestToMethodItem {
	public String controllerName;
    public String methodName;
    public String requestType;
    public String requestUrl;
    public Class<?>[] methodParmaTypes;    
    public String getControllerName() {
		return controllerName;
	}
	public void setControllerName(String controllerName) {
		this.controllerName = controllerName;
	}
	public String getMethodName() {
		return methodName;
	}
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	public String getRequestType() {
		return requestType;
	}
	public void setRequestType(String requestType) {
		this.requestType = requestType;
	}
	public String getRequestUrl() {
		return requestUrl;
	}
	public void setRequestUrl(String requestUrl) {
		this.requestUrl = requestUrl;
	}
	public Class<?>[] getMethodParmaTypes() {
		return methodParmaTypes;
	}
	public void setMethodParmaTypes(Class<?>[] methodParmaTypes) {
		this.methodParmaTypes = methodParmaTypes;
	}
    public RequestToMethodItem(String requestUrl, String requestType, String controllerName, String requestMethodName, Class<?>[] methodParmaTypes){
        this.requestUrl = requestUrl;
        this.requestType = requestType;
        this.controllerName = controllerName;
        this.methodName = requestMethodName;
        this.methodParmaTypes = methodParmaTypes;
    }
}

二、单元测试

写两个http接口用于测试


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
	@GetMapping(value = "/test1")
	@ResponseBody
	public void test1(Integer a) {
	}
	
	@PostMapping(value = "/test2")
	@ResponseBody
	public void test2(Integer a,Integer b) {
	}
}

测试单元


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import cn.hutool.JSON.jsONUtil; //hutool是一个很方便的工具包
@SpringBootTest
@RunWith(SpringRunner.class)
public class Test {	
    @Autowired
    WebApplicationContext applicationContext;
    
	@org.junit.Test
	public void index() {
		List<RequestToMethodItem> requestToMethodItemList = new ArrayList<RequestToMethodItem>();
        RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
		for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods
				.entrySet()) {
			RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey();				
			RequestMethodsRequestCondition methodCondition = requestMappingInfo.getMethodsCondition();
			PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
			HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry.getValue();
			
			// 请求类型
			String requestType = methodCondition.getMethods().toString();			
			// 请求路径
			String requestUrl = patternsCondition.getPatterns().iterator().next();
			// 控制器名称
			String controllerName = mappingInfoValue.getBeanType().toString();
			// 请求方法名
			String requestMethodName = mappingInfoValue.getMethod().getName();
			// 请求参数
			Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes();
			
			// Spring通过BasicErrorController进行统一的异常处理,不计入这些API
			if("class org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController".equals(controllerName)) {
				continue;
			}
			
			RequestToMethodItem item = new RequestToMethodItem(requestUrl, requestType, controllerName,
					requestMethodName, methodParamTypes);
			requestToMethodItemList.add(item);						
		}
		
		System.out.println(JSONUtil.toJsonStr(requestToMethodItemList));		
	}
}

测试结果

在这里插入图片描述

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Springboot项目如何获取所有的接口

本文链接: https://www.lsjlt.com/news/135384.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Springboot项目如何获取所有的接口
    目录Springboot项目获取所有接口获取项目下所有http接口的信息一、接口信息类二、单元测试Springboot项目获取所有接口 @Autowired private We...
    99+
    2022-11-12
  • springboot如何获取接口下所有实现类
    目录springboot获取接口下所有实现类springboot动态调用实现类1、添加接口2、创建实现类3、获取实现类的相关接口 springboot获取接口下所有实现类 ...
    99+
    2022-11-13
  • 使用SpringBoot获取所有接口的路由
    目录SpringBoot获取所有接口的路由Springboot部分路由生效问题记录SpringBoot获取所有接口的路由 @Autowired WebApplicatio...
    99+
    2022-11-12
  • Java如何获取接口所有的实现类
    目录Java获取接口所有的实现类反射获取接口的所有实现类总结Java获取接口所有的实现类 最近因业务需求,要实现NodeRed服务后端化,为使各个节点的解析进行插件化(NodeRed...
    99+
    2023-01-09
    Java接口 Java实现类 Java获取接口实现类
  • springboot启动时如何获取端口和项目名
    目录springboot启动获取端口和项目名背景踩坑使用效果springboot配置项目运行端口号这个方法极其简洁springboot启动获取端口和项目名 背景 项目启动每次都要手动...
    99+
    2022-11-12
  • SpringBoot项目jar发布后如何获取jar包所在目录路径
    目录SpringBoot项目jar发布获取jar包所在目录路径SpringBoot打可执行jar运行时输出文件路径问题SpringBoot项目jar发布获取jar包所在目录路径 ...
    99+
    2022-11-12
  • springboot之如何获取项目目录路径
    目录springboot获取项目目录路径springboot获取resources目录资源文件9种方式方式一方式二方式三方式四(重要)方式五(重要)方式六(重要)方式七方式八方式九总...
    99+
    2023-05-20
    springboot项目路径 springboot获取目录路径 springboot目录路径获取
  • 如何在SpringBoot+Freemarker中获取项目根目录
    目录在Freemarker中获取项目根目录在Freemark模板引擎路径的几种设置方法在SpringMVC中我们想返回视图是怎么做的在SpringBoot中springboot不推荐...
    99+
    2022-11-12
  • python如何获取目录下所有子目录
    Python可以使用os模块来获取目录下的所有子目录。方法一:使用os.walk()函数os.walk()函数可以遍历指定目录下的所...
    99+
    2023-09-27
    python
  • SpringBoot项目中jar发布获取jar包所在目录路径的最佳方法
     示例代码: //第一种 File path = new File(ResourceUtils.getURL("classpath:").g...
    99+
    2022-11-13
  • SpringBoot如何连接MySQL获取数据写后端接口
    这篇文章将为大家详细讲解有关SpringBoot如何连接MySQL获取数据写后端接口,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1.新建项目2.添加依赖<dependency> &...
    99+
    2023-06-25
  • springboot接口如何多次获取request中的body内容
    1. 概述 在使用springboot开发接口时,会将参数转化为Bean,用来进行参数的自动校验。同时也想获取request中原始body报文进行验签(防止报文传输过程中被篡改)。 ...
    99+
    2022-11-12
  • mysql连接如何在Java项目中进行获取
    mysql连接如何在Java项目中进行获取?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。一、传统的连接方式:首先在 src 目录下创建名为 lib 的文件夹,导入数据库驱动...
    99+
    2023-05-31
    java mysql ava
  • 如何获取Maven项目的版本号
    目录Jar包的秘密 Maven资源插件过滤 Spring Boot提供 总结 目前大多数Spring Boot项目都会打成Jar包,所以什么War包、Ear包的就先不摸索了。 Jar...
    99+
    2022-11-12
  • 如何获取所有spring管理的bean
    目录获取所有spring管理的beanIOC容器使用ListableBeanFactory接口使用Spring Boot Actuator小结一下spring管理bean的原理使用s...
    99+
    2022-11-12
  • java中如何获取map的所有键
    在Java中,可以使用`keySet()`方法获取Map的所有键。该方法返回一个Set集合,其中包含Map中所有的键。以下是一个示例...
    99+
    2023-08-31
    java
  • JavaScript如何获取对象的所有键
    这篇文章主要为大家展示了“JavaScript如何获取对象的所有键”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“JavaScript如何获取对象的所有键”这篇文章吧。获取对象的所有键cosnt&...
    99+
    2023-06-27
  • linux Shell如何获取某目录下所有文件夹的名称
    这篇文章主要为大家展示了“linux Shell如何获取某目录下所有文件夹的名称”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“linux Shell如何获取某目录下所有文件夹的名称”这篇文章吧。...
    99+
    2023-06-09
  • SpringBoot项目如何使用hutool工具进行http接口调用
    本文小编为大家详细介绍“SpringBoot项目如何使用hutool工具进行http接口调用”,内容详细,步骤清晰,细节处理妥当,希望这篇“SpringBoot项目如何使用hutool工具进行http接口调用”文章能帮助大家解决疑惑,下面跟...
    99+
    2023-06-30
  • golang 如何获取map所有key的方式
    最佳方式:根据map的长度,新建一个数组,遍历map逐个压入 方法1(效率很高): func getKeys1(m map[int]int) []int { // 数组默认长度...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作