温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot项目怎么获取所有的接口

发布时间:2021-09-13 07:28:05 来源:亿速云 阅读:377 作者:chen 栏目:开发技术

这篇文章主要讲解了“Springboot项目怎么获取所有的接口”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Springboot项目怎么获取所有的接口”吧!

目录
  • Springboot项目获取所有接口

  • 获取项目下所有http接口的信息

    • 一、接口信息类

    • 二、单元测试

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项目怎么获取所有的接口

感谢各位的阅读,以上就是“Springboot项目怎么获取所有的接口”的内容了,经过本文的学习后,相信大家对Springboot项目怎么获取所有的接口这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI