spring-boot 禁用swagger的方法,spring-bootswagger

來源:互聯網
上載者:User

spring-boot 禁用swagger的方法,spring-bootswagger

在使用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")@EnableSwagger2public 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的時候才會生效,其他值或不設值,都不會生效的。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.