spring-boot 禁用swagger的方法
在使用spring-boot開發(fā)的時(shí)候,我們很多時(shí)候會(huì)使用swagger作為api文檔輸出??梢栽赨I界面上看到api的路徑,參數(shù)等等。
當(dāng)然,作為開發(fā)環(huán)境是很方便的,但是上生產(chǎn)環(huán)境的時(shí)候,我們需要把swagger禁掉。怎么通過(guò)配置文件的方法來(lái)禁用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
關(guān)鍵就是這里的 @ConditionalOnProperty
這里的屬性key是 swagger.enable ,havingValue 是期望值,只有在值等于期望值的時(shí)候,才會(huì)生效。也就是說(shuō),swagger.enable只能為true的時(shí)候才會(huì)生效,其他值或不設(shè)值,都不會(huì)生效的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot無(wú)法訪問/static下靜態(tài)資源的解決
這篇文章主要介紹了SpringBoot無(wú)法訪問/static下靜態(tài)資源的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringMVC實(shí)現(xiàn)文件上傳下載的全過(guò)程
對(duì)于上傳功能,我們?cè)陧?xiàng)目中是經(jīng)常會(huì)用到的,比如用戶注冊(cè)的時(shí)候,上傳用戶頭像,這個(gè)時(shí)候就會(huì)使用到上傳的功能,而對(duì)于下載使用場(chǎng)景也很常見,下面這篇文章主要給大家介紹了關(guān)于SpringMVC實(shí)現(xiàn)文件上傳下載的相關(guān)資料,需要的朋友可以參考下2022-01-01
詳解spring boot 使用application.properties 進(jìn)行外部配置
這篇文章主要介紹了詳解spring boot 使用application.properties 進(jìn)行外部配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
詳解maven的setting配置文件中mirror和repository的區(qū)別
這篇文章主要介紹了詳解maven的setting配置文件中mirror和repository的區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
生成PDF全攻略之在已有PDF上添加內(nèi)容的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇生成PDF全攻略之在已有PDF上添加內(nèi)容的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
Spring Bean初始化及銷毀多種實(shí)現(xiàn)方式
這篇文章主要介紹了Spring Bean初始化及銷毀多種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11

