当前位置:网站首页>[swagger close] The production environment closes the swagger method
[swagger close] The production environment closes the swagger method
2022-07-31 05:53:00 【m0_67391401】
swagger3 关闭配置(快捷方式)
配置参考
springfox:
documentation:
# 总开关(同时设置auto-startup=false,否则/v3/api-docs等接口仍能继续访问)
enabled: false
auto-startup: false
swagger-ui:
enabled: false
配置原理
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency>
springfox.boot.starter.autoconfigure.OpenApiAutoConfiguration
springfox.boot.starter.autoconfigure.SpringfoxConfigurationProperties
swagger2 关闭配置
swagger2 关闭主要是根据条件使swagger 配置不再生效,如
方法一:@ConditionalOnProperty
Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2 extends WebMvcConfigurationSupport {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
.contact(new Contact("java乐园", "https://xxx.com", "[email protected]"))
.version("1.0")
.description("User API 描述")
.build();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}
}
application.yml 中配置如下关闭:
swagger:
# 只要不是true就不启用
enable: false
其他基于Conditional的方式
方法二 @Profile
原理跟第一个差不多,只是判断条件不同(profile判断配置文件,也即的参数)
Configuration
@EnableSwagger2
@Profile("!prod")
public class Swagger2 extends WebMvcConfigurationSupport {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
.contact(new Contact("java乐园", "https://xxx.com", "[email protected]"))
.version("1.0")
.description("User API 描述")
.build();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}
}
方法三 @Value 配置Docket 失效办法
Configuration
@EnableSwagger2
public class Swagger2 extends WebMvcConfigurationSupport {
@Value("{swagger2.enable:false}")
private boolean enableSwagger;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(enableSwagger)
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
.contact(new Contact("java乐园", "https://xxx.com", "[email protected]"))
.version("1.0")
.description("User API 描述")
.build();
}
@Override
protected 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工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢
碎片化NFT(Fractional NFT)
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
leetcode-1833. 雪糕的最大数量(排序+贪心)
leetcode-每日一题558. 四叉树交集(分治递归)
Linux中mysql密码修改方法(亲测可用)
The process and specific code of sending SMS verification code using flask framework
02 【el和data的两种写法 MVVM模型】
What is the difference between NFT and digital collection?
npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案
【JVM加载】---类加载机制
随机推荐
How to distinguish big and small endian in C language
【云原生】微服务Nacos的简单介绍与使用
Linux modify MySQL database password
gin框架学习-Gin框架和Gorm框架搭建一个简单的API微服务
Linux中mysql密码修改方法(亲测可用)
对递归的一些感悟
了解SSRF,这一篇就足够了
对于输出点是时间戳的渗透测试方法(以Oracle数据库为例)
(Crypto必备干货)详细分析目前NFT的几大交易市场
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
数字孪生将成为进入“元宇宙”一项重要的途径
闭包(三)----执行环境
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
Regular Expression Basics
Detailed explanation of pointers in C language
碎片化NFT(Fractional NFT)
vulhub靶场学习日记hackme2
leetcode-每日一题735. 行星碰撞(栈模拟)
What is an EVM Compatible Chain?
let和const命令