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<String> EXCLUDE_CLASS = Arrays.asList("BasicErrorController", "ApiResourceController");
public void saveOrUpdateAllApi(String modelName) {
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = requestMappingHandlerMapping.getHandlerMethods();
for (Map.Entry<RequestMappingInfo, HandlerMethod> 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<String> patterns = patternsCondition.getPatterns();
String servletPath = patterns.stream().findFirst().orElse("");
//请求方式
String requestMethod = "";
RequestMethodsRequestCondition methodsCondition = requestMappingInfo.getMethodsCondition();
Set<RequestMethod> 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());
}
}
}
}
springboot项目启动时获取所有的api接口
最新推荐文章于 2024-11-13 23:26:52 发布