Springboot项目如何获取所有的接口
目录
- Springboot项目获取所有接口
- 获取项目下所有http接口的信息
- 一、接口信息类
- 二、单元测试
Springboot项目获取所有接口
@Autowiredprivate WebApplicationContext applicationContext; @Overridepublic List getAllUrl() {RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class); // 获取url与类和方法的对应信息Mapmap = 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 = "https://www.it610.com/test1") @ResponseBody public void test1(Integer a) { } @PostMapping(value = "https://www.it610.com/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 { @AutowiredWebApplicationContext applicationContext; @org.junit.Test public void index() {ListrequestToMethodItemList = 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 = https://www.it610.com/article/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进行统一的异常处理,不计入这些APIif("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项目如何获取所有的接口】以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
推荐阅读
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- 如何寻找情感问答App的分析切入点
- Activiti(一)SpringBoot2集成Activiti6
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus使用queryWrapper如何实现复杂查询
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 如何在Mac中的文件选择框中打开系统隐藏文件夹
- 漫画初学者如何学习漫画背景的透视画法(这篇教程请收藏好了!)
- java中如何实现重建二叉树