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

测试结果

在这里插入图片描述

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

相关文章

  • SpringBoot+Ajax+redis实现隐藏重要接口地址的方法

    SpringBoot+Ajax+redis实现隐藏重要接口地址的方法

    这篇文章主要介绍了SpringBoot+Ajax+redis实现隐藏重要接口地址,本篇文章主要讲诉使用SpringBoot项目配合Ajax和redis实现隐藏重要接口地址,这里我以隐藏秒杀地址为例,需要的朋友可以参考下
    2024-03-03
  • Spring在SingleTon模式下的线程安全详解

    Spring在SingleTon模式下的线程安全详解

    这篇文章主要介绍了Spring在SingleTon模式下的线程安全详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • java随机字符补充版

    java随机字符补充版

    今天在zuidaimai看到一个java随机字符生成demo,正好要用,但发现不完整,重新整理一下,分享给有需要的朋友
    2014-01-01
  • SpringAop中AspectJ框架的切入点表达式

    SpringAop中AspectJ框架的切入点表达式

    这篇文章主要介绍了SpringAop中AspectJ框架的切入点表达式,AspectJ是一个基于Java语言的AOP框架,Spring2.0以后新增了对AspectJ切点表达式支持,@AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面,需要的朋友可以参考下
    2023-08-08
  • java发送url请求获取返回值的二种方法

    java发送url请求获取返回值的二种方法

    这篇文章主要介绍了java发送url请求获取返回值的二种方法,需要的朋友可以参考下
    2014-03-03
  • Java中包装类介绍与其注意事项

    Java中包装类介绍与其注意事项

    Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很多的不便,所以在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数据类型对应的类统称为包装类,有些地方也翻译为外覆类或数据类型类。
    2017-02-02
  • java ant包中的org.apache.tools.zip实现压缩和解压缩实例详解

    java ant包中的org.apache.tools.zip实现压缩和解压缩实例详解

    这篇文章主要介绍了java ant包中的org.apache.tools.zip实现压缩和解压缩实例详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • 如何使用Idea中的 Deployment 实现打包自动部署

    如何使用Idea中的 Deployment 实现打包自动部署

    这篇文章主要介绍了使用Idea中的 Deployment 实现打包自动部署,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-08-08
  • Spring security 如何开放 Swagger 访问权限

    Spring security 如何开放 Swagger 访问权限

    这篇文章主要介绍了Spring security 如何开放 Swagger 访问权限操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • java emoji表情存储的解决方法

    java emoji表情存储的解决方法

    这篇文章主要为大家详细介绍了java emoji表情存储的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06

最新评论