spring-boot如何禁用swagger

小编给大家分享一下spring-boot如何禁用swagger,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

十多年的大方网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整大方建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“大方网站设计”,“大方网站推广”以来,每个客户项目都认真落实执行。

在使用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的时候才会生效,其他值或不设值,都不会生效的。

以上是“spring-boot如何禁用swagger”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!

标题名称:spring-boot如何禁用swagger
当前链接:https://www.cdcxhl.com/article42/iggohc.html

成都网站建设公司_创新互联,为您提供软件开发App设计网站策划营销型网站建设网站设计全网营销推广

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

商城网站建设