Spring Boot 禁用 Swagger 的三种方式

/**

  • @author sunny chen

  • @version V1.0

  • @Package com.dc.config

  • @date 2018/1/16 17:33

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

*/

@Configuration

@EnableSwagger2

public class Swagger2Config extends WebMvcConfigurerAdapter {

@Value(“${swagger.enable}”)

private Boolean enable;

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.enable(enable)

.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”);

}

}


禁用方法2:

======

使用注解 @Profile({“dev”,“test”}) 表示在开发或测试环境开启,而在生产关闭。(推荐使用)

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.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.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

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

public class Swagger2Config extends WebMvcConfigurerAdapter {

@Bean

public Docket createRestApi() {

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
9789302)]

[外链图片转存中…(img-BcvrgEW8-1714959789303)]

[外链图片转存中…(img-RLmJtWvN-1714959789303)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 12
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值