• springboot项目启动时获取所有的api接口


    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
  • 相关阅读:
    Nacos配置管理
    mysql创建子账户
    osgearth3.2加载地球
    【2012NOIP普及组】T4. 文化之旅 试题解析
    spring事务出现的超卖问题
    iOS 17中的Safari配置文件改变了游戏规则,那么如何设置呢
    【MindSpore1.5.0】利用MindSpore1.5.0构建STN模型时出现cudaMalloc failed错误
    HTML5网页前端设计-作业一
    5. 文档的字段映射
    Golang数组和slice
  • 原文地址:https://blog.csdn.net/qq_25271459/article/details/126932449