您好,登錄后才能下訂單哦!
這篇文章主要講解了“Springboot項目怎么獲取所有的接口”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Springboot項目怎么獲取所有的接口”吧!
Springboot項目獲取所有接口
獲取項目下所有http接口的信息
一、接口信息類
二、單元測試
@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接口的相關信息
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項目怎么獲取所有的接口這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。