最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot項(xiàng)目如何獲取所有的接口

 更新時(shí)間:2021年09月11日 10:29:39   作者:小石潭記丶  
這篇文章主要介紹了Springboot項(xiàng)目如何獲取所有的接口,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Springboot項(xiàng)目獲取所有接口

@Autowired
private WebApplicationContext applicationContext; 
@Override
public List getAllUrl() {
    RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
    // 獲取url與類和方法的對(duì)應(yīng)信息
    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();
        //獲取當(dāng)前方法所在類名
        Class<?> bean = method.getBeanType();
        //使用反射獲取當(dāng)前類注解內(nèi)容
        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;
}

獲取項(xiàng)目下所有http接口的信息

一、接口信息類

新建一個(gè)類用于存放http接口的相關(guān)信息

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

二、單元測(cè)試

寫兩個(gè)http接口用于測(cè)試

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

測(cè)試單元

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是一個(gè)很方便的工具包
@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();
			
			// 請(qǐng)求類型
			String requestType = methodCondition.getMethods().toString();			
			// 請(qǐng)求路徑
			String requestUrl = patternsCondition.getPatterns().iterator().next();
			// 控制器名稱
			String controllerName = mappingInfoValue.getBeanType().toString();
			// 請(qǐng)求方法名
			String requestMethodName = mappingInfoValue.getMethod().getName();
			// 請(qǐng)求參數(shù)
			Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes();
			
			// Spring通過BasicErrorController進(jìn)行統(tǒng)一的異常處理,不計(jì)入這些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));		
	}
}

測(cè)試結(jié)果

在這里插入圖片描述

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解析Java中的Field類和Method類

    解析Java中的Field類和Method類

    這篇文章主要介紹了Java中的Field類和Method類,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-12-12
  • 詳解Java無需解壓直接讀取Zip文件和文件內(nèi)容

    詳解Java無需解壓直接讀取Zip文件和文件內(nèi)容

    本篇文章主要介紹了詳解Java無需解壓直接讀取Zip文件和文件內(nèi)容,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Java實(shí)現(xiàn)求小于n的質(zhì)數(shù)的3種方法

    Java實(shí)現(xiàn)求小于n的質(zhì)數(shù)的3種方法

    這篇文章主要介紹了Java實(shí)現(xiàn)求小于n的質(zhì)數(shù)的3種方法,本文給出了根據(jù)定義去求解、平方根、找規(guī)律三種解法,需要的朋友可以參考下
    2015-03-03
  • java集合進(jìn)行排序的方式總結(jié)

    java集合進(jìn)行排序的方式總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于java集合進(jìn)行排序的兩種方式總結(jié),有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-08-08
  • java之CSV大批量數(shù)據(jù)入庫的實(shí)現(xiàn)

    java之CSV大批量數(shù)據(jù)入庫的實(shí)現(xiàn)

    本文主要介紹了java之CSV大批量數(shù)據(jù)入庫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • 使用Java操作Parquet文件的基本步驟

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

    Parquet 是一個(gè)強(qiáng)大的列式存儲(chǔ)格式,適用于大數(shù)據(jù)場(chǎng)景,能夠高效地進(jìn)行數(shù)據(jù)壓縮、查詢和存儲(chǔ),在 Java 中使用 Apache Spark 讀取和寫入 Parquet 文件是一項(xiàng)常見的任務(wù),本文給大家介紹了在 Java 中使用 Spark 來讀取和寫入 Parquet 文件的基本步驟,需要的朋友可以參考下
    2025-03-03
  • SpringBoot Test及注解的使用詳解

    SpringBoot Test及注解的使用詳解

    這篇文章主要介紹了SpringBoot Test及注解的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • FreeMarker配置(Configuration)

    FreeMarker配置(Configuration)

    所有與該configuration 對(duì)象關(guān)聯(lián)的模版實(shí)例都就可以通過獲得to_upper 轉(zhuǎn)換器,company 來獲得字符串,因此你不需要再一次次的往root 中添加這些變量了。如果你往root 添加同名的變量,那么你新添加的變量將會(huì)覆蓋之前的共享變量。
    2016-04-04
  • 詳解Sentinel流量控制限流框架的原理與使用

    詳解Sentinel流量控制限流框架的原理與使用

    Sentinel 是一個(gè)高可用、高擴(kuò)展、高穩(wěn)定性的開源流量控制和熔斷降級(jí)框架,可以在分布式系統(tǒng)中實(shí)現(xiàn)實(shí)時(shí)的流量控制,下面就來和大家聊聊是具體如何操作的吧
    2023-05-05
  • Spring源碼之事件監(jiān)聽機(jī)制詳解(@EventListener實(shí)現(xiàn)方式)

    Spring源碼之事件監(jiān)聽機(jī)制詳解(@EventListener實(shí)現(xiàn)方式)

    這篇文章主要介紹了Spring源碼之事件監(jiān)聽機(jī)制(@EventListener實(shí)現(xiàn)方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08

最新評(píng)論

陵川县| 宜黄县| 文昌市| 公主岭市| 鄱阳县| 高安市| 民县| 安国市| 蛟河市| 金昌市| 鄢陵县| 东阿县| 高台县| 扬州市| 兴业县| 军事| 平阴县| 五指山市| 马边| 铁力市| 大英县| 资溪县| 镇原县| 文登市| 民乐县| 陇南市| 望都县| 抚顺县| 哈尔滨市| 新邵县| 遵义市| 子长县| 洪洞县| 洛南县| 萨嘎县| 洞头县| 镇江市| 兴安盟| 容城县| 广南县| 深圳市|