Spring Boot 禁用 Swagger 的三种方式

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.builders.PathSelectors;

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;

/**

  • @author sunny chen

  • @version V1.0

  • @Package com.dc.config

  • @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(RequestHandlerSelectors.basePackage(“com.dc.controller”))

.paths(PathSelectors.any())

//.paths(PathSelectors.none())

.build();

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

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

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

.termsOfServiceUrl(“”)

.contact(new Contact(“陈永佳 chen867647213@163.com”, “”, “https://blog.csdn.net/Mrs_chens”))

.version(“1.0”)

.build();

}

/**

  • swagger ui资源映射

  • @param registry

*/

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler(“swagger-ui.html”)

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

registry.addResourceHandler(“/webjars/**”)

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

}

/**

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

  • @param registry

*/

@Override

public void addViewControllers(ViewControllerRegistry registry) {

registry.addRedirectViewController(“/api-docs”,“/swagger-ui.html”);

}

}


禁用方法3:

======

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

package com.dc.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import springfox.documentation.builders.ApiInfoBuilder;

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

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;

/**

  • @author sunny chen

  • @version V1.0

  • @Package com.dc.config

  • @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(RequestHandlerSelectors.basePackage(“com.dc.controller”))

.paths(PathSelectors.any())

//.paths(PathSelectors.none())

.build();

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

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

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

.termsOfServiceUrl(“”)

.contact(new Contact(“陈永佳 chen867647213@163.com”, “”, “https://blog.csdn.net/Mrs_chens”))

.version(“1.0”)

.build();

}

/**

  • swagger ui资源映射

  • @param registry

*/

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler(“swagger-ui.html”)

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

registry.addResourceHandler(“/webjars/**”)

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

}

/**

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

  • @param registry

*/

@Override

public void addViewControllers(ViewControllerRegistry registry) {

registry.addRedirectViewController(“/api-docs”,“/swagger-ui.html”);

}

}

#Swagger lock

swagger:
enabled: true

作者:Sunny_Chen
链接:https://juejin.cn/post/7012779158849716261
来源:掘金

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 项目中,可以通过添加配置来禁用 Swagger。 1. 针对整个应用禁用 Swagger 可以在应用的 `application.properties` 文件中添加以下配置: ```properties springfox.documentation.enabled=false ``` 或者在应用的启动类上添加 `@EnableSwagger2` 注解,并重写 `addResourceHandlers` 方法,将 Swagger UI 的访问路径重定向到 `/error`: ```java import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableSwagger2 public class SwaggerConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/") .setCachePeriod(0); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(0); registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/") .setCachePeriod(0); registry.addResourceHandler("/swagger-resources/**") .addResourceLocations("classpath:/META-INF/swagger-resources/") .setCachePeriod(0); } } ``` 2. 针对指定的 API 禁用 Swagger 可以在 API 的实现类上添加 `@ApiIgnore` 注解,例如: ```java @RestController @RequestMapping("/api") @ApiIgnore public class MyController { // ... } ``` 这样,Swagger 将不会显示该 API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值