Swagger分离生产环境和发布环境

现在application.yaml中分离环境

spring:
  profiles:
    active: dev

此时运行的时候生产环境

swagger中我们有两种方式,一种是直接用@Value注解的方法,简单粗暴

@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {


    //直接通过value获取到配置文件的值
    //对应的application-dev.properties文件中会写上swagger.enable=boolean类型的值
    @Value("${swagger.enable}")
    private  boolean enable;



    //配置swagger的bean实例
    @Bean
    public Docket docket(Environment environment){


      return  new Docket(DocumentationType.SWAGGER_2)
              //配置swagger信息
              .apiInfo(apiInfo())
              .enable(enable)
              .select()
              .apis(RequestHandlerSelectors.basePackage("com.kuang.controller"))
              //paths过滤路径
              //ant设置过滤的路径
              //.paths(PathSelectors.ant("kuang/**"))
              .build();
    }
    //配置swagger信息的类,通过DocumentationType.SWAGGER_2.apiInfo调用这个方法
    //这些配置的大部分是没有什么用的,都是用来显示的,但是就算没用我们也要赔,因为我们点进去ApiInfo这个方法
    //我们就会发现它没有set方法,只有get方法,所以就算没用只是用来显示,我们也要将其配置好,就算是乱输入,也要输入
    //不然你们懂的
    public ApiInfo apiInfo(){
        Contact contact = new Contact("蒋锦华", "https://i.csdn.net/#/uc/profile", "1661259340@qq.com");
        return new ApiInfo("蒋锦华的swagger日志",
                "你尽管努力,其他的交给天意",
                "1.0",
                "https://i.csdn.net/#/uc/profile",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }


}

我们要在我们的application-dev.properties文件中找到@Value("${swagger.enable}")中的swagger.enable配置类
在这里插入图片描述
true为开启false为关闭

还有一种是Profiles的方式

@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {

    //配置swagger的bean实例
    @Bean
    public Docket docket(Environment environment){

        //Profiles直接获取环
        Profiles profiles = Profiles.of("dev","test");
        //获取环境,点进源码,查看方法以及参数值
        //返回值为boolean acceptsProfiles(Profiles var1);boolea类型
        boolean enable = environment.acceptsProfiles(profiles);

        //Docket的参数点进Docket源码中,我们重写的是这个方法,所以我们也要传入这个参数
        //DocumentationType.SWAGGER_2参数的很多的值可以配置
      return  new Docket(DocumentationType.SWAGGER_2)
              //配置swagger信息
              .apiInfo(apiInfo())
              .enable(enable)
              .select()
              //RequestHandlerSelectors扫描方式
              //basePackage扫描包
              //any全部扫描
              //none全部不扫描
              //withClassAnnotation扫描class的注解 ,参数是一个注解的反射对象 如 RestController.class
              //withMethodAnnotation扫描方法上的注解 如 GetMapper.class
              .apis(RequestHandlerSelectors.basePackage("com.kuang.controller"))
              //paths过滤路径
              //ant设置过滤的路径
              //.paths(PathSelectors.ant("kuang/**"))
              .build();
    }
    //配置swagger信息的类,通过DocumentationType.SWAGGER_2.apiInfo调用这个方法
    //这些配置的大部分是没有什么用的,都是用来显示的,但是就算没用我们也要赔,因为我们点进去ApiInfo这个方法
    //我们就会发现它没有set方法,只有get方法,所以就算没用只是用来显示,我们也要将其配置好,就算是乱输入,也要输入
    //不然你们懂的
    public ApiInfo apiInfo(){
        Contact contact = new Contact("蒋锦华", "https://i.csdn.net/#/uc/profile", "1661259340@qq.com");
        return new ApiInfo("蒋锦华的swagger日志",
                "你尽管努力,其他的交给天意",
                "1.0",
                "https://i.csdn.net/#/uc/profile",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }


}

具体原理里面有注解,我写代码会把我理解到的都写进注解里
把博客当笔记记录一下我们的demo大部分就是这样的,如果没听懂的我推荐大家去B站看狂神的讲解的swagger,通俗易懂

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值