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在开发环境中开启,在发布环境下关闭

posted @ 2022-03-10 09:49  从0开始丿  阅读(3445)  评论(0编辑  收藏  举报