• Swagger实现在生产环境中使用,在发布时不使用



    Swagger实现在生产环境中使用,在发布时不使用

    • 判断是不是生产环境 flag = false
    • 注入enable(flag)

    1、配置三个SpringBoot文件

    • application.properties
    # SpringBoot2.6.x以上需要配置,springfox3.0.x以上需要配置
    spring.mvc.pathmatch.matching-strategy=ant_path_matcher
    # 激活哪个环境
    spring.profiles.active=dev
    
    • 1
    • 2
    • 3
    • 4
    • application-dev.properties
    # 生产环境
    server.port=8081
    
    • 1
    • 2
    • application-pro.properties
    # 发布环境
    server.port=8082
    
    • 1
    • 2

    2、编写SwaggerConfig配置类

    package com.sgz.swagger.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.Environment;
    import org.springframework.core.env.Profiles;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    /**
     * 日期:2022/8/15 - 15:54
     * 需求:配置Swagger
     */
    @Configuration  // 标识是一个配置页面
    @EnableSwagger2 // 标识开启Swagger,会扫描当前所在包以及子包中所有的注解
    public class SwaggerConfig {
    
    
        /**
         * 创建Docket类型的对象,并使用Spring容器bean管理,Docket是Swagger中的全局配置对象
         *
         * @return
         */
        @Bean
        public Docket docket(Environment environment) {
    
            // 设置要显示的Swagger环境,获取到则8081可以获取API,获取不到则8082无法获取API(这个)
            Profiles profiles = Profiles.of("dev","test");
            // 获取项目的环境,判断是否处在自己设定的环境中(这个)
            boolean flag = environment.acceptsProfiles(profiles);
            System.out.println(flag);(这个)
    
            // API帮助文档的描述信息
            ApiInfo apiInfo = new ApiInfoBuilder()
                    .contact(new Contact("我", "https://www.baidu.com", "xxxxxx@qq.com"))  // 作者信息
                    .title("我的Swagger API开发文档")    // 文档标题
                    .description("我的文档描述")    // 文档描述
                    .version("1.1")         // 版本号
                    .build();   // 构建
    
            // 创建Docket类型的对象
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo)     // API帮助文档的描述信息
                    .enable(flag)   // 是否启动Swagger,false不能在浏览器中访问,true可以在浏览器中访问(这个)
                    .groupName("我")   // 配置分组
                    .select()   // 获取Docket中的选择器
                    .apis(RequestHandlerSelectors.basePackage("com.sgz.swagger.controller"))    // 设置扫描包以及子包中的注解
                    .paths(PathSelectors.regex("/swagger/.*"))  // 过滤路径,使用正则表达式约束生成API文档的路径地址
                    .build();
        }
    
    }
    
    
    • 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
  • 相关阅读:
    免费的Git图形界面工具sourceTree介绍
    【MM32F5270】RT-Thread SPI 驱动适配指南
    实例036:算素数
    Java面试必考点第01讲:技术人职业发展路径
    大型语言模型的推理演算
    CNN网络测试集准确率始终无法提高
    概率论与数理统计学习:随机事件(一)——知识总结与C语言实现案例
    测试用例的设计方法(全):等价类划分方法
    Windows下安装并访问mysql容器
    std : : map
  • 原文地址:https://blog.csdn.net/s17856147699/article/details/126350754