springboot禁用swagger spring-boot 禁用swagger的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot禁用swagger spring-boot 禁用swagger的方法

彼岸   2021-03-28 我要评论
想了解spring-boot 禁用swagger的方法的相关内容吗,彼岸在本文为您仔细讲解springboot禁用swagger的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot禁用swagger,spring,boot,swagger,下面大家一起来学习吧。

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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们