Hi ~ 小老弟开始转公众号啦,欢迎大家来指点迷津呀
前排注意:这里涉及到了swagger的,没用到的可以自行更改。
关于动态feign调用的,可以参考这里~ 关于动态创建Feign Client的问题
主入口:
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMethod;
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.util.*;
public List<Map<String, Object>> getUrl() {
List<Map<String, Object>> list = new ArrayList<>();
RequestMappingHandlerMapping mapping = webApplicationContext.getBean(RequestMappingHandlerMapping.class);
Map<RequestMappingInfo, HandlerMethod> handlerMethods = mapping.getHandlerMethods();
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
// 这里去掉swagger的接口
String className = entry.getValue().getMethod().getDeclaringClass().getName();
if (ObjectUtils.isEmpty(className) || className.contains("SwaggerConfig")) {
continue;
}
Map<String, Object> map = new HashMap<>(16);
map.putAll(getSwaggerProperties(entry.getValue()));
map.put("requestType", getRequestType(entry.getKey().getMethodsCondition()));
map.put("requestServer", webApplicationContext.getEnvironment().getProperty("spring.application.name"));
// 一个方法可能对应多个url
PatternsRequestCondition patternsCondition = entry.getKey().getPatternsCondition();
for (String url : patternsCondition.getPatterns()) {
map.put("requestUrl", getRequestUrl(url));
list.add(map);
}
}
return list;
}
getSwaggerProperties:
private Map<String, Object> getSwaggerProperties(HandlerMethod handlerMethod) {
Map<String, Object> map = new HashMap<>(16);
// 完整接口信息:类名信息 + 接口方法信息
String requestName = "";
// 接口方法补充描述
String remark = "";
// 类名的备注信息
Api api = handlerMethod.getMethod().getDeclaringClass().getAnnotation(Api.class);
if (handlerMethod.getMethod().getDeclaringClass().isAnnotationPresent(Api.class) && Objects.nonNull(api)) {
requestName = api.value();
}
// 接口方法名的备注信息
ApiOperation apiOperation = handlerMethod.getMethodAnnotation(ApiOperation.class);
if (handlerMethod.hasMethodAnnotation(ApiOperation.class) && Objects.nonNull(apiOperation)) {
requestName = ObjectUtils.isEmpty(requestName) ? apiOperation.value() : requestName + "_" + apiOperation.value();
remark = ObjectUtils.isEmpty(apiOperation.notes()) ? "" : apiOperation.notes();
} else {
requestName = ObjectUtils.isEmpty(requestName) ? handlerMethod.getMethod().getName() : requestName + "_" + handlerMethod.getMethod().getName();
}
map.put("requestName", requestName);
map.put("remark", remark);
return map;
}
getRequestType:
private String getRequestType(RequestMethodsRequestCondition methodsCondition) {
// 如果多个,改为一个POST
String requestType = null;
int count = 0;
for (RequestMethod requestMethod : methodsCondition.getMethods()) {
requestType = requestMethod.toString();
count++;
}
return requestType == null || count > 1 ? "POST" : requestType;
}
getRequestUrl:
private String getRequestUrl(String url) {
String separator = "/";
String separator2 = "//";
String separator3 = "///";
String contextPath = webApplicationContext.getEnvironment().getProperty("server.servlet.context-path");
String requestUrl;
if (!ObjectUtils.isEmpty(contextPath) && !separator.equals(contextPath)) {
requestUrl = contextPath + url;
if (requestUrl.contains(separator3) || requestUrl.contains(separator2)) {
requestUrl = requestUrl.replace(separator3, separator);
requestUrl = requestUrl.replace(separator2, separator);
}
} else {
requestUrl = url;
}
return requestUrl;
}
测试执行结果:
{
"requestName": "类名备注-测试方法1",
"requestServer": "alya-test",
"requestType": "GET",
"requestUrl": "/test/{id}",
"remark": "测试方法备注信息1"
}, {
"requestName": "类名备注-测试方法2",
"requestServer": "alya-test",
"requestType": "POST",
"requestUrl": "/test",
"remark": "测试方法备注信息2"
}
最后,关于动态feign调用的,可以参考这里~ 关于动态创建Feign Client的问题