欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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();
        //使用反射獲取當前類注解內(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;
}

獲取項目下所有http接口的信息

一、接口信息類

新建一個類用于存放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;
    }
}

二、單元測試

寫兩個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();
			// 請求參數(shù)
			Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes();
			
			// Spring通過BasicErrorController進行統(tǒng)一的異常處理,不計入這些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));		
	}
}

測試結(jié)果

在這里插入圖片描述

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

相關(guān)文章

  • 詳談spring中bean注入無效和new創(chuàng)建對象的區(qū)別

    詳談spring中bean注入無效和new創(chuàng)建對象的區(qū)別

    這篇文章主要介紹了spring中bean注入無效和new創(chuàng)建對象的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 詳談Java泛型中T和問號(通配符)的區(qū)別

    詳談Java泛型中T和問號(通配符)的區(qū)別

    下面小編就為大家?guī)硪黄斦凧ava泛型中T和問號(通配符)的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Java源碼解析ConcurrentHashMap的初始化

    Java源碼解析ConcurrentHashMap的初始化

    今天小編就為大家分享一篇關(guān)于Java源碼解析ConcurrentHashMap的初始化,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • java判斷ip是否為指定網(wǎng)段示例

    java判斷ip是否為指定網(wǎng)段示例

    這篇文章主要介紹了java判斷ip是否為指定網(wǎng)段示例方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • Spring cloud Eureka注冊中心搭建的方法

    Spring cloud Eureka注冊中心搭建的方法

    這篇文章主要介紹了Spring cloud Eureka注冊中心搭建的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Spring?Batch批處理框架操作指南

    Spring?Batch批處理框架操作指南

    Spring?Batch?是?Spring?提供的一個數(shù)據(jù)處理框架。企業(yè)域中的許多應用程序需要批量處理才能在關(guān)鍵任務環(huán)境中執(zhí)行業(yè)務操作,這篇文章主要介紹了Spring?Batch批處理框架操作指南,需要的朋友可以參考下
    2022-07-07
  • Guava Cache的使用簡介

    Guava Cache的使用簡介

    這篇文章主要介紹了Guava Cache的使用簡介,幫助大家更好的理解和學習使用Guava Cache,感興趣的朋友可以了解下
    2021-03-03
  • Java線程讓步_動力節(jié)點Java學院整理

    Java線程讓步_動力節(jié)點Java學院整理

    yield()的作用是讓步。它能讓當前線程由“運行狀態(tài)”進入到“就緒狀態(tài)”,從而讓其它具有相同優(yōu)先級的等待線程獲取執(zhí)行權(quán)。下面通過本文給大家介紹Java線程讓步的相關(guān)知識,需要的朋友參考下吧
    2017-05-05
  • Spring中的自動裝配機制詳解

    Spring中的自動裝配機制詳解

    這篇文章主要介紹了Spring中的自動裝配機制詳解,自動裝配就是會通過Spring的上下文為你找出相應依賴項的類,通俗的說就是Spring會在上下文中自動查找,并自動給Bean裝配與其相關(guān)的屬性,需要的朋友可以參考下
    2023-08-08
  • Java工作隊列代碼詳解

    Java工作隊列代碼詳解

    這篇文章主要介紹了Java工作隊列代碼詳解,涉及Round-robin 轉(zhuǎn)發(fā),消息應答(messageacknowledgments),消息持久化(Messagedurability)等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11

最新評論