Spring Boot 禁用 Swagger 的三种方式

时间:2025-03-20 22:15:03

import ;

import ;

import ;

import ;

import ;

import ;

import ;

import ;

import ;

import ;

import ;

import ;

import ;

import ..EnableSwagger2;

/**

  • @author sunny chen

  • @version V1.0

  • @Package

  • @date 2018/1/16 17:33

  • @Description: 主要用途:开启在线接口文档和添加相关配置

*/

@Configuration

@EnableSwagger2

@Profile({“dev”,“test”})

public class Swagger2Config extends WebMvcConfigurerAdapter {

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.select()

.apis((“”))

.paths(())

//.paths(())

.build();

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title(“auth系统数据接口文档”)

.description(“此系统为新架构Api说明文档”)

.termsOfServiceUrl(“”)

.contact(new Contact(“陈永佳 chen867647213@”, “”, “/Mrs_chens”))

.version(“1.0”)

.build();

}

/**

  • swagger ui资源映射

  • @param registry

*/

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

(“”)

.addResourceLocations(“classpath:/META-INF/resources/”);

(“/webjars/**”)

.addResourceLocations(“classpath:/META-INF/resources/webjars/”);

}

/**

  • 路径映射,浏览器中使用/api-docs访问

  • @param registry

*/

@Override

public void addViewControllers(ViewControllerRegistry registry) {

(“/api-docs”,“/”);

}

}


禁用方法3:

======

使用注解 @ConditionalOnProperty(name = “”, havingValue = “true”) 然后在测试配置或者开发配置中 添加 = true 即可开启,生产环境不填则默认关闭 Swagger.

package ;

import ;

import ;

import ;

import ;

import ;

import ;

import ;

import 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 ;

import ;

import ;

import ;

import ;

import ;

import ..EnableSwagger2;

/**

  • @author sunny chen

  • @version V1.0

  • @Package

  • @date 2018/1/16 17:33

  • @Description: 主要用途:开启在线接口文档和添加相关配置

*/

@Configuration

@EnableSwagger2

@ConditionalOnProperty(name =“enabled” ,prefix = “swagger”,havingValue = “true”,matchIfMissing = true)

public class Swagger2Config extends WebMvcConfigurerAdapter {

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.select()

.apis((“”))

.paths(())

//.paths(())

.build();

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title(“auth系统数据接口文档”)

.description(“此系统为新架构Api说明文档”)

.termsOfServiceUrl(“”)

.contact(new Contact(“陈永佳 chen867647213@”, “”, “/Mrs_chens”))

.version(“1.0”)

.build();

}

/**

  • swagger ui资源映射

  • @param registry

*/

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

(“”)

.addResourceLocations(“classpath:/META-INF/resources/”);

(“/webjars/**”)

.addResourceLocations(“classpath:/META-INF/resources/webjars/”);

}

/**

  • 路径映射,浏览器中使用/api-docs访问

  • @param registry

*/

@Override

public void addViewControllers(ViewControllerRegistry registry) {

(“/api-docs”,“/”);

}

}

#Swagger lock

swagger:
enabled: true

作者:Sunny_Chen
链接:/post/7012779158849716261
来源:掘金