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

 更新时间:2021年09月11日 10:29:39   作者:小石潭记丶  
这篇文章主要介绍了Springboot项目如何获取所有的接口,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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));		
	}
}

测试结果

在这里插入图片描述

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 解析Java中的Field类和Method类

    解析Java中的Field类和Method类

    这篇文章主要介绍了Java中的Field类和Method类,是Java入门学习中的基础知识,需要的朋友可以参考下
    2015-12-12
  • 详解Java无需解压直接读取Zip文件和文件内容

    详解Java无需解压直接读取Zip文件和文件内容

    本篇文章主要介绍了详解Java无需解压直接读取Zip文件和文件内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Java实现求小于n的质数的3种方法

    Java实现求小于n的质数的3种方法

    这篇文章主要介绍了Java实现求小于n的质数的3种方法,本文给出了根据定义去求解、平方根、找规律三种解法,需要的朋友可以参考下
    2015-03-03
  • java集合进行排序的方式总结

    java集合进行排序的方式总结

    在本篇文章里小编给大家整理的是一篇关于java集合进行排序的两种方式总结,有兴趣的朋友们可以学习参考下。
    2021-08-08
  • java之CSV大批量数据入库的实现

    java之CSV大批量数据入库的实现

    本文主要介绍了java之CSV大批量数据入库的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • 使用Java操作Parquet文件的基本步骤

    使用Java操作Parquet文件的基本步骤

    Parquet 是一个强大的列式存储格式,适用于大数据场景,能够高效地进行数据压缩、查询和存储,在 Java 中使用 Apache Spark 读取和写入 Parquet 文件是一项常见的任务,本文给大家介绍了在 Java 中使用 Spark 来读取和写入 Parquet 文件的基本步骤,需要的朋友可以参考下
    2025-03-03
  • SpringBoot Test及注解的使用详解

    SpringBoot Test及注解的使用详解

    这篇文章主要介绍了SpringBoot Test及注解的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • FreeMarker配置(Configuration)

    FreeMarker配置(Configuration)

    所有与该configuration 对象关联的模版实例都就可以通过获得to_upper 转换器,company 来获得字符串,因此你不需要再一次次的往root 中添加这些变量了。如果你往root 添加同名的变量,那么你新添加的变量将会覆盖之前的共享变量。
    2016-04-04
  • 详解Sentinel流量控制限流框架的原理与使用

    详解Sentinel流量控制限流框架的原理与使用

    Sentinel 是一个高可用、高扩展、高稳定性的开源流量控制和熔断降级框架,可以在分布式系统中实现实时的流量控制,下面就来和大家聊聊是具体如何操作的吧
    2023-05-05
  • Spring源码之事件监听机制详解(@EventListener实现方式)

    Spring源码之事件监听机制详解(@EventListener实现方式)

    这篇文章主要介绍了Spring源码之事件监听机制(@EventListener实现方式),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08

最新评论