package cn.com.qmth.mps.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 INDEX_BY_MAPPING = Maps.newConcurrentMap(); private static final Map INDEX_BY_METHOD = Maps.newConcurrentMap(); private static Set 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 getApiInfoSet() { return apiInfoSet; } @Override public void run(ApplicationArguments args) throws Exception { Map map = requestMappingHandlerMapping .getHandlerMethods(); for (Map.Entry 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 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); } } }