• 如何swagger关闭及给swagger加参数信息


    项目的swagger方便研发人员调试,上线之后需要将swagger关闭这时候需要给原来的@Configuration注解换成@ConditionlOnProperty注解及可。注解参数信息

    @ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
    
    • 1

    若swagger想加入额外参数类型,代码如下

    首先是正常的swagger配置正常界面及调试方法界面

    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.service.Parameter;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /******************************
     * 用途说明: Swagger配置
     ******************************/
    @EnableSwagger2
    //@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
    @Configuration
    public class SwaggerConfig {
    
        //生成swagger文档相关配置
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.test"))//扫描com路径下的api文档
                    .paths(PathSelectors.any())//路径判断
                    .build();
        }
    
        //用途说明: 生成swagger文档相关配置
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("swagger服务接口测试平台")
                    .contact(new Contact("dexi.chi攻城狮", "https://test", ""))
                    .version("1.0.0.RELEASE")
                    .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

    在这里插入图片描述

    改造后代码

    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.service.Parameter;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /******************************
     * 用途说明: Swagger配置
     ******************************/
    @EnableSwagger2
    //@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
    @Configuration
    public class SwaggerConfig {
    
        //生成swagger文档相关配置
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.test"))//扫描com路径下的api文档
                    .paths(PathSelectors.any())//路径判断
                    .build()
                    .globalOperationParameters(getParameterList());
        }
    
        //用途说明: 生成swagger文档相关配置
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("swagger服务接口测试平台")
                    .contact(new Contact("dexi.chi攻城狮", "https://test", ""))
                    .version("1.0.0.RELEASE")
                    .build();
        }
    
        private List<Parameter> getParameterList() {
            ParameterBuilder parameterBuilder = new ParameterBuilder();
            List<Parameter> pars = new ArrayList<>();
    
            pars.add(parameterBuilder.name("userNo")
                    .description("校验人员工号")
                    .modelRef(new ModelRef("string"))
                    .parameterType("header")
                    .required(true).build());
    
            return pars;
        }
    }
    
    
    • 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

    在这里插入图片描述

  • 相关阅读:
    Logo制作方法大公开:初学者也能学会的Logo设计教程
    Linux.文本文件操作
    springboot+shiro+swagger+redis+vue+element完成权限系统
    限定input输入框输入类型(只能输入数字。。)
    在进行自动化测试,遇到验证码的问题,怎么办?
    图像处理学习笔记-04-频率域滤波01-基本知识
    Python-类中的变量和方法
    计算机网络:数据链路层设备 网桥与交换机
    44.日期交叉问题(品牌活动天数计算)
    IP协议总结
  • 原文地址:https://blog.csdn.net/weixin_43949154/article/details/133796837