Springboot项目怎么获取所有的接口
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要讲解了"Springboot项目怎么获取所有的接口",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Springboot项目怎么获取所有的接口
千家信息网最后更新 2026年11月02日Springboot项目怎么获取所有的接口
这篇文章主要讲解了"Springboot项目怎么获取所有的接口",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Springboot项目怎么获取所有的接口"吧!
目录
Springboot项目获取所有接口
获取项目下所有http接口的信息
一、接口信息类
二、单元测试
Springboot项目获取所有接口
@Autowiredprivate WebApplicationContext applicationContext; @Overridepublic List getAllUrl() { RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class); // 获取url与类和方法的对应信息 Map map = mapping.getHandlerMethods(); 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;@Controllerpublic 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 requestToMethodItemList = new ArrayList(); RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class); Map handlerMethods = requestMappingHandlerMapping.getHandlerMethods(); for (Map.Entry 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)); }} 测试结果

感谢各位的阅读,以上就是"Springboot项目怎么获取所有的接口"的内容了,经过本文的学习后,相信大家对Springboot项目怎么获取所有的接口这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
接口
项目
信息
方法
测试
内容
单元
注解
学习
类名
两个
参数
名称
就是
工具
工具包
思路
情况
所在
控制器
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技股
重生之互联网科技帝国
科技企业和互联网企业
互联网 科技
九亚互联网科技
南京欣网互联网科技
左右互联网络科技
互联网金融监管科技
居家互联网科技有限公司
金融科技和互联网金融
新华互联网科技2019
成都铂石互联网科技做什么的
黑科技闪耀互联网之光
南博望互联网科技有限公司
郑州欣桑互联网科技有限公司
普陀区互联网教育科技管理办法
同城互联网络科技
互联网科技品牌
互联网科技对化妆品行业
知域互联网科技有限公司官网
中关村互联网科技集团
上海喔噻互联网科技
奇酷互联网络科技公司
深圳天众互联网科技公司
互联网 新科技
伍佰万互联网科技怎么赚钱
江苏互联网科技小贷
广州有谷有贝互联网科技有限公司
世界互联网科技成果奖
互联网时代科技型公司的机会