In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to get all the interfaces of the Springboot project". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to get all the interfaces of the Springboot project".
Catalogue
The Springboot project gets all interfaces
Get the information of all http interfaces under the project
Interface information class
II. Unit testing
The Springboot project gets all interfaces @ Autowiredprivate WebApplicationContext applicationContext; @ Overridepublic List getAllUrl () {RequestMappingHandlerMapping mapping = applicationContext.getBean (RequestMappingHandlerMapping.class); / / gets the corresponding information between url and classes and methods Map map = mapping.getHandlerMethods (); List list = new ArrayList (); for (Map.Entry m: map.entrySet ()) {Map map1 = new HashMap (); RequestMappingInfo info = m.getKey (); HandlerMethod method = m.getValue () / / get the class name of the current method Class bean = method.getBeanType (); / / use reflection to get the annotated content of the current class Api api = bean.getAnnotation (Api.class); RequestMapping requestMapping = bean.getAnnotation (RequestMapping.class); String [] value = requestMapping.value () Map1.put ("parent", value [0]) / / get the annotations on the method and the annotation values 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 ()); / / Class name map1.put ("method", method.getMethod (). GetName ()); / / method name RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition (); for (RequestMethod requestMethod: methodsCondition.getMethods ()) {map1.put ("type", requestMethod.toString ()) } list.add (map1);} return list;} get the information of all http interfaces under the project 1. Interface information class
Create a new class to store information about the http interface
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;}} II. Unit test
Write two http interfaces for testing
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 @ Controllerpublic class TestController {@ GetMapping (value = "/ test1") @ ResponseBody public void test1 (Integer a) {} @ PostMapping (value = "/ test2") @ ResponseBody public void test2 (Integer AME Integer b) {}}
Test unit
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 is a very convenient toolkit @ SpringBootTest@RunWith (SpringRunner.class) public class Test {@ Autowired WebApplicationContext applicationContext; @ org.junit.Test public void index () {List requestToMethodItemList = new ArrayList () RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean (RequestMappingHandlerMapping.class); Map handlerMethods = requestMappingHandlerMapping.getHandlerMethods (); for (Map.Entry requestMappingInfoHandlerMethodEntry: handlerMethods .entrySet ()) {RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey (); RequestMethodsRequestCondition methodCondition = requestMappingInfo.getMethodsCondition () PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition (); HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry.getValue (); / / request type String requestType = methodCondition.getMethods () .toString () / / request path String requestUrl = patternsCondition.getPatterns () .iterator () .next (); / / Controller name String controllerName = mappingInfoValue.getBeanType () .toString () / / request method name String requestMethodName = mappingInfoValue.getMethod () .getName (); / / request parameter Class [] methodParamTypes = mappingInfoValue.getMethod () .getParameterTypes () / / Spring performs unified exception handling through BasicErrorController, excluding these 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));}}
Test result
Thank you for your reading, the above is the content of "how to get all the interfaces of the Springboot project". After the study of this article, I believe you have a deeper understanding of the problem of how to obtain all the interfaces of the Springboot project. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.