springboot-swagger:配置開關


承接:springboot-swagger:配置掃描接口

1 關閉swagger

1.1 修改SwaggerConfig

通過enable()方法配置是否啟用swagger,如果是false,swagger將不能在瀏覽器中訪問了

src/main/java/com/lv/config/SwaggerConfig.java

package com.lv.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 //開啟Swagger2
public class SwaggerConfig {
    //配置了Swagger的Docket的bean實例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(false)//enable是否啟動Swagger,如果為false,則Swagger不能在瀏覽器中訪問
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lv.controller"))
                //.paths(PathSelectors.ant("/lv/**"))
                .build();
    }

    //配置swagger信息 : apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("工藤新一", "https://www.cnblogs.com/lv1024/", "1148397597@qq.com");
        return new ApiInfo(
                "工藤新一的swaggerAPI文檔",
                "心機之蛙一直摸你肚子",
                "v1.0",
                "https://www.cnblogs.com/lv1024/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());
    }
}

1.2 啟動程序測試

訪問失敗,說明swagger已關閉

2 根據環境設置是否啟動swagger

設置swagger只在開發環境中使用,在發布環境下不使用

2.1 編寫三個配置文件

在resources目錄下新建application-dev.properties 文件代表開發環境和application-pro.properties 文件代表發布環境

src/main/resources/application.properties

spring.profiles.active=dev

src/main/resources/application-dev.properties

server.port=8081

src/main/resources/application-pro.properties

server.port=8082

2.2 修改SwaggerConfig

增加了當前環境的判斷,並把enable()的參數設為變量

src/main/java/com/lv/config/SwaggerConfig.java

//配置了Swagger的Docket的bean實例
@Bean
public Docket docket(Environment environment){

    //設置要顯示的swagger環境
    Profiles profiles = Profiles.of("dev", "test");
    //通過environment.acceptsProfiles判斷是否處在自己設定的環境當中
    boolean flag = environment.acceptsProfiles(profiles);

    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .enable(flag)//enable是否啟動Swagger,如果為false,則Swagger不能在瀏覽器中訪問
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.lv.controller"))
            //.paths(PathSelectors.ant("/lv/**"))
            .build();
}

2.3 啟動程序測試

現在是在dev開發環境下,訪問8081端口

確認swagger開啟,接下來修改application.properties配置文件中的環境配置,改為發布環境

src/main/resources/application.properties

spring.profiles.active=pro

重啟程序,現在是在pro發布環境,訪問8082端口

確認swagger是關閉狀態,實現了wagger在開發環境中開啟,在發布環境下關閉


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM