本文讲述获取当前项目的所有restful请求的详细信息,含url、接口描述、请求方式等信息,主要用到了swagger的相关特性,最终返回的数据格式如下:
[{
"className": "cn.miao.controller.InfoController",
"classDesc": "InfoController",
"methodName": "getTimeStamp",
"methodDesc": "获取时间戳",
"methodURL": "/getTimeStamp",
"requestType": "GET"
},...]
具体代码如下:
package cn.miao.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
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 java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@RestController
@Api(value = "TestControllerApi", description = "获取url")
@RequestMapping("/get/api/demo")
@Slf4j
public class TestController {
@Autowired
WebApplicationContext applicationContext;
@RequestMapping(value = "/getAllUrl", method = RequestMethod.GET)
public List> getAllUrl() {
List> resList = new ArrayList<>();
RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
Map map = requestMappingHandlerMapping.getHandlerMethods();
for (Map.Entry mappingInfoHandlerMethodEntry : map.entrySet()) {
Map resultMap = new LinkedHashMap<>();
RequestMappingInfo requestMappingInfo = mappingInfoHandlerMethodEntry.getKey();
HandlerMethod handlerMethod = mappingInfoHandlerMethodEntry.getValue();
resultMap.put("className", handlerMethod.getMethod().getDeclaringClass().getName());
Annotation[] parentAnnotations = handlerMethod.getBeanType().getAnnotations();
for (Annotation annotation : parentAnnotations) {
if (annotation instanceof Api) {
Api api = (Api) annotation;
resultMap.put("classDesc", api.value());
} else if (annotation instanceof RequestMapping) {
RequestMapping requestMapping = (RequestMapping) annotation;
if (null != requestMapping.value() && requestMapping.value().length > 0) {
resultMap.put("classURL", requestMapping.value()[0]);
}
}
}
resultMap.put("methodName", handlerMethod.getMethod().getName());
Annotation[] annotations = handlerMethod.getMethod().getDeclaredAnnotations();
if (annotations != null) {
for (Annotation annotation : annotations) {
if (annotation instanceof ApiOperation) {
ApiOperation methodDesc = (ApiOperation) annotation;
String desc = methodDesc.value();
resultMap.put("methodDesc", desc);
}
}
}
PatternsRequestCondition p = requestMappingInfo.getPatternsCondition();
for (String url : p.getPatterns()) {
resultMap.put("methodURL", url);
}
RequestMethodsRequestCondition methodsRequestCondition = requestMappingInfo.getMethodsCondition();
for (RequestMethod requestMethod : methodsRequestCondition.getMethods()) {
resultMap.put("requestType", requestMethod.toString());
}
resList.add(resultMap);
}
return resList;
}
}
Original: https://www.cnblogs.com/miaoying/p/13528941.html
Author: miaoying
Title: 获取spring web项目的所有url请求信息
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/541824/
转载文章受原作者版权保护。转载请注明原作者出处!