import io.swagger.annotations.ApiOperation;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import org.springframework.stereotype.Service;
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 java.lang.reflect.Method;
import java.util.*;
/**
* @author
*/
@Service
@FieldDefaults(level = AccessLevel.PRIVATE)
@RequiredArgsConstructor
public class SysApiInfoService {
final RequestMappingHandlerMapping requestMappingHandlerMapping;
/**
* 排除的类 BasicErrorController ApiResourceController
*/
List EXCLUDE_CLASS = Arrays.asList("BasicErrorController", "ApiResourceController");
public void saveOrUpdateAllApi(String modelName) {
Map handlerMethodMap = requestMappingHandlerMapping.getHandlerMethods();
for (Map.Entry handlerMethodEntry : handlerMethodMap.entrySet()) {
HandlerMethod handlerMethod = handlerMethodEntry.getValue();
RequestMappingInfo requestMappingInfo = handlerMethodEntry.getKey();
//类
Class> clazz = handlerMethod.getBeanType();
if (EXCLUDE_CLASS.contains(clazz.getSimpleName())) {
continue;
}
//方法
Method method = handlerMethod.getMethod();
//servletPath
PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
Set patterns = patternsCondition.getPatterns();
String servletPath = patterns.stream().findFirst().orElse("");
//请求方式
String requestMethod = "";
RequestMethodsRequestCondition methodsCondition = requestMappingInfo.getMethodsCondition();
Set methods = methodsCondition.getMethods();
if (methods.size() == 1) {
requestMethod = methods.stream().findFirst().get().name();
}
if (method.isAnnotationPresent(ApiOperation.class)) {
ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
System.out.println("swagger接口名字"+apiOperation.value());
}
}
}
}
- 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