SynIntegratedInterfaceImpl.java
4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.pipi.qa.service.impl;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.method.annotation.ModelFactory;
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 com.pipi.qa.service.SynIntegratedInterface;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Service
public class SynIntegratedInterfaceImpl implements SynIntegratedInterface{
@Autowired
WebApplicationContext applicationContext;
@Override
public List<Map<String, String>> getAllURL() {
List<Map<String, String>> resultList = new ArrayList<>();
RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext
.getBean(RequestMappingHandlerMapping.class);
// 获取url与类和方法的对应信息
Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
for (Map.Entry<RequestMappingInfo, HandlerMethod> mappingInfoHandlerMethodEntry : map.entrySet()) {
Map<String, String> resultMap = new LinkedHashMap<>();
RequestMappingInfo requestMappingInfo = mappingInfoHandlerMethodEntry.getKey();
HandlerMethod handlerMethod = mappingInfoHandlerMethodEntry.getValue();
MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
for (MethodParameter parameter:methodParameters) {
Class<?> parameterType = parameter.getParameterType();
String nameForParameter = ModelFactory.getNameForParameter(parameter);
logger.info("类型"+parameterType.getName()+"---->缺省的modelkey是"+nameForParameter);
}
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]);// 类URL
}
}
}
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();
String notes = methodDesc.notes();
resultMap.put("methodDesc", desc);// 接口描述
resultMap.put("notes", notes);// 注释、说明
}
}
}
PatternsRequestCondition p = requestMappingInfo.getPatternsCondition();
for (String url : p.getPatterns()) {
resultMap.put("methodURL", url);// 请求URL
}
RequestMethodsRequestCondition methodsCondition = requestMappingInfo.getMethodsCondition();
for (RequestMethod requestMethod : methodsCondition.getMethods()) {
resultMap.put("requestType", requestMethod.toString());// 请求方式:POST/PUT/GET/DELETE
}
resultList.add(resultMap);
}
return resultList;
}
}