|
@@ -1,148 +0,0 @@
|
|
|
-package cn.com.qmth.scancentral.support;
|
|
|
-
|
|
|
-import java.lang.reflect.Method;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
-
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.boot.ApplicationArguments;
|
|
|
-import org.springframework.boot.ApplicationRunner;
|
|
|
-import org.springframework.core.annotation.AnnotationUtils;
|
|
|
-import org.springframework.core.annotation.Order;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
-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 com.google.common.collect.Maps;
|
|
|
-import com.google.common.collect.Sets;
|
|
|
-
|
|
|
-import io.swagger.annotations.ApiOperation;
|
|
|
-
|
|
|
-@Component
|
|
|
-@Order(100)
|
|
|
-public class ApiInfoHolder implements ApplicationRunner {
|
|
|
-
|
|
|
- private static final Map<String, ApiInfo> INDEX_BY_MAPPING = Maps.newConcurrentMap();
|
|
|
-
|
|
|
- private static final Map<Method, ApiInfo> INDEX_BY_METHOD = Maps.newConcurrentMap();
|
|
|
-
|
|
|
- private static Set<ApiInfo> apiInfoSet = Sets.newHashSet();
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private RequestMappingHandlerMapping requestMappingHandlerMapping;
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 通过方法获取ApiInfo
|
|
|
- *
|
|
|
- * @author
|
|
|
- * @param method
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static ApiInfo getApiInfo(Method method) {
|
|
|
- return INDEX_BY_METHOD.get(method);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通过mapping获取ApiInfo
|
|
|
- *
|
|
|
- * @author
|
|
|
- * @param mapping
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static ApiInfo getApiInfo(String mapping) {
|
|
|
- return INDEX_BY_MAPPING.get(mapping);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取@ApiId注解的ApiInfo集合
|
|
|
- *
|
|
|
- * @author
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Set<ApiInfo> getApiInfoSet() {
|
|
|
- return apiInfoSet;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void run(ApplicationArguments args) throws Exception {
|
|
|
-
|
|
|
- Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping
|
|
|
- .getHandlerMethods();
|
|
|
-
|
|
|
- for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : map.entrySet()) {
|
|
|
- RequestMappingInfo requestMappingInfo = entry.getKey();
|
|
|
- HandlerMethod handlerMethod = entry.getValue();
|
|
|
-
|
|
|
- Class<?> beanType = handlerMethod.getBeanType();
|
|
|
-
|
|
|
- RequestMapping requestMappingAnnotationOfClass = AnnotationUtils
|
|
|
- .findAnnotation(beanType, RequestMapping.class);
|
|
|
-
|
|
|
- RequestMapping requestMappingAnnotationOfMethod = handlerMethod
|
|
|
- .getMethodAnnotation(RequestMapping.class);
|
|
|
-
|
|
|
- WithoutStackTrace withoutStackTrace = handlerMethod
|
|
|
- .getMethodAnnotation(WithoutStackTrace.class);
|
|
|
-
|
|
|
- ApiOperation apiOperation = handlerMethod.getMethodAnnotation(ApiOperation.class);
|
|
|
-
|
|
|
- RequestMethodsRequestCondition requestMethodsRequestCondition = requestMappingInfo
|
|
|
- .getMethodsCondition();
|
|
|
- Set<RequestMethod> requestMethodSet = requestMethodsRequestCondition.getMethods();
|
|
|
-
|
|
|
- String[] mappingURIsOfClass = null;
|
|
|
- String[] mappingURIsOfMethod = null;
|
|
|
-
|
|
|
- if (null != requestMappingAnnotationOfClass) {
|
|
|
- mappingURIsOfClass = requestMappingAnnotationOfClass.path();
|
|
|
- }
|
|
|
- if (null != requestMappingAnnotationOfMethod) {
|
|
|
- mappingURIsOfMethod = requestMappingAnnotationOfMethod.path();
|
|
|
- }
|
|
|
-
|
|
|
- String mappingIdentifyOfClass = null;
|
|
|
- String mappingIdentifyOfMethod = null;
|
|
|
-
|
|
|
- if (null != mappingURIsOfClass) {
|
|
|
- mappingIdentifyOfClass = StringUtils.join(mappingURIsOfClass, ",");
|
|
|
- }
|
|
|
- if (null != mappingURIsOfMethod) {
|
|
|
- mappingIdentifyOfMethod = StringUtils.join(mappingURIsOfMethod, ",");
|
|
|
- }
|
|
|
-
|
|
|
- String methods = StringUtils.join(requestMethodSet, ",");
|
|
|
-
|
|
|
- String mapping = StringUtils.join("[", mappingIdentifyOfClass, "][",
|
|
|
- mappingIdentifyOfMethod, "][", methods, "]");
|
|
|
-
|
|
|
- PatternsRequestCondition patternsRequestCondition = requestMappingInfo
|
|
|
- .getPatternsCondition();
|
|
|
-
|
|
|
- String requestPath = StringUtils.join(patternsRequestCondition.getPatterns(), ",");
|
|
|
-
|
|
|
- ApiInfo apiInfo = new ApiInfo();
|
|
|
- if (null != apiOperation) {
|
|
|
- apiInfo.setDescription(apiOperation.value());
|
|
|
- }
|
|
|
- apiInfo.setHttpMethod(methods);
|
|
|
- apiInfo.setMapping(mapping);
|
|
|
- apiInfo.setRequestPath(requestPath);
|
|
|
- apiInfo.setBeanType(beanType);
|
|
|
- if (null != withoutStackTrace) {
|
|
|
- apiInfo.setWithoutStackTrace(withoutStackTrace.value());
|
|
|
- }
|
|
|
-
|
|
|
- INDEX_BY_METHOD.put(handlerMethod.getMethod(), apiInfo);
|
|
|
-
|
|
|
- INDEX_BY_MAPPING.put(mapping, apiInfo);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|