Spring Boot 禁用 Swagger 的三种方式


方法

==


禁用方法1:

======

使用注解 @Value() 推荐使用

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

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/”);
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

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

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后总结我的面试经验

2021年的金三银四一眨眼就到了,对于很多人来说是跳槽的好机会,大厂面试远没有我们想的那么困难,摆好心态,做好准备,你也可以的。

另外,面试中遇到不会的问题不妨尝试讲讲自己的思路,因为有些问题不是考察我们的编程能力,而是逻辑思维表达能力;最后平时要进行自我分析与评价,做好职业规划,不断摸索,提高自己的编程能力和抽象思维能力。

BAT面试经验

实战系列:Spring全家桶+Redis等

其他相关的电子书:源码+调优

面试真题:


《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!
0359297)]

BAT面试经验

实战系列:Spring全家桶+Redis等

[外链图片转存中…(img-af0t3Epo-1712070359298)]

其他相关的电子书:源码+调优

[外链图片转存中…(img-SYmBihCq-1712070359298)]

面试真题:

[外链图片转存中…(img-gVgcSBVQ-1712070359299)]

[外链图片转存中…(img-AfXBpYSJ-1712070359299)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!

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

余额充值