当前位置: 移动技术网 > IT编程>开发语言>Java > spring-boot 禁用swagger的方法

spring-boot 禁用swagger的方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

在使用spring-boot开发的时候,我们很多时候会使用swagger作为api文档输出。可以在ui界面上看到api的路径,参数等等。

当然,作为开发环境是很方便的,但是上生产环境的时候,我们需要把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.requesthandlerselectors;
import springfox.documentation.schema.modelref;
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;

/**
 * created by bane.shi.
 * user: bane.shi
 * date: 2017/12/28
 * time: 下午2:15
 */
@configuration
@conditionalonproperty(prefix = "swagger",value = {"enable"},havingvalue = "true")
@enableswagger2
public class swaggerconfiguration {

  @bean
  public docket swagger(){
    return new docket(documentationtype.swagger_2)
        .groupname("default")
        .apiinfo(new apiinfobuilder().title("ssp school api").version("1.0.0").build())
        .select()
        .apis(requesthandlerselectors.basepackage("com.fclassroom.ssp.school"))
        .build()
        .globaloperationparameters(globaloperationparameters());
  }


  private list<parameter> globaloperationparameters(){
    list<parameter> parameters = new arraylist<>();
    // parameters.add(new parameterbuilder().name("access-token").description("access-token").required(false).parametertype("header").modelref(new modelref("string")).build());
    return parameters;
  }
}

如果要开启swagger,在配置文件中加上

swagger.enable=true

关键就是这里的 @conditionalonproperty

这里的属性key是 swagger.enable ,havingvalue 是期望值,只有在值等于期望值的时候,才会生效。也就是说,swagger.enable只能为true的时候才会生效,其他值或不设值,都不会生效的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网