Springboot项目如何获取所有的接口
更新时间:2021年9月11日 16:00 点击:1789
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)); } }
测试结果
以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。
相关文章
解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题
这篇文章主要介绍了解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-28- 这篇文章主要介绍了c# 三种方法调用WebService接口的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-07
- 这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
- 这篇文章主要介绍了详解springBoot启动时找不到或无法加载主类解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
- 这篇文章主要介绍了vue接口请求加密实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-12
- 这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
- 这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
Spring Boot项目@RestController使用重定向redirect方式
这篇文章主要介绍了Spring Boot项目@RestController使用重定向redirect方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-02- 这篇文章主要介绍了Springboot+TCP监听服务器搭建过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-10-28
- 这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
- 这篇文章主要介绍了C#简单了解接口(Interface)使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-12-08
详解SpringBoot之访问静态资源(webapp...)
这篇文章主要介绍了详解SpringBoot之访问静态资源(webapp...),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-14- 这篇文章主要介绍了SpringBoot接口接收json参数解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-19
springboot中使用@Transactional注解事物不生效的坑
这篇文章主要介绍了springboot中使用@Transactional注解事物不生效的原因,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26- 这篇文章介绍的是一个小技巧来获取node.js项目根目录,这个技巧非常实用。有需要的朋友们可以参考借鉴,下面来一起看看吧。...2016-10-02
- 这篇文章主要介绍了C# Rx的主要接口深入理解的相关资料,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了Feign接口方法返回值设置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-07-08
- 这篇文章主要介绍了springboot多模块包扫描问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
- 这篇文章主要介绍了vue设置全局访问接口API地址操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-14
- 在日常开发中,总会接触到各种接口,前后端数据传输接口,第三方业务平台接口,下面这篇文章主要给大家介绍了关于如何设计一个安全的API接口的相关资料,需要的朋友可以参考下...2021-08-12