如何在生产环境禁用swagger
pringMVC集成springfox-swagger2和springfox-swagger-ui很简单,只需要两步:
(1)pom中添加依赖
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>${springfox-swagger.version}</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>${springfox-swagger.version}</version>
- </dependency>
(2)添加Swagger的配置类:
- @Configuration
- @EnableSwagger2
- @EnableWebMvc
- @ComponentScan("com.XXX.controller")
- public class SwaggerConfig{
- }
然后就可以通过http://localhost/swagger-ui.html看到项目中所有的接口信息了,通过http://localhost/v2/api-docs就能看到json数据。
转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/78280051
但是,如何在生产环境禁用这些api文档呢?试了很多种方式,最终找到一个简单实用的办法:
- @Configuration
- @EnableSwagger2
- @EnableWebMvc
- @ComponentScan("com.XXX.controller")
- public class SwaggerConfig{
- @Autowired
- ConfigService configService;
- @Bean
- public Docket customDocket() {
- if(configService.getServerEnv() == ServerEnvEnum.ONLINE) {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfoOnline())
- .select()
- .paths(PathSelectors.none())//如果是线上环境,添加路径过滤,设置为全部都不符合
- .build();
- }else {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo());
- }
- }
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("XXX系统")
- .description("XXX系统接口")
- .license("")
- .licenseUrl("")
- .termsOfServiceUrl("")
- .version("1.0.0")
- .contact(new Contact("","", ""))
- .build();
- }
- private ApiInfo apiInfoOnline() {
- return new ApiInfoBuilder()
- .title("")
- .description("")
- .license("")
- .licenseUrl("")
- .termsOfServiceUrl("")
- .version("")
- .contact(new Contact("","", ""))
- .build();
- }
- }
参考:http://blog.csdn.net/w4hechuan2009/article/details/68892718
swagger必须要跟springmvc在同一个context才行,springmvc只是spring的一个子context。如果swagger让spring的context加载,那么swagger的那些url用springmvc的拦截器是拦截不到的!
所以,两种解决办法:
如果是使用注解的方式:
(1)spring-mvc的配置:
- <!-- 使用Annotation自动注册Bean,只扫描@Controller -->
- <context:component-scan base-package="com.inspur.eyun.yunbx" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- <context:include-filter type="assignable" expression="com.inspur.eyun.yunbx.swagger.SwaggerConfig"/>
- </context:component-scan>
注意要把swagger的配置加进来,同时:
(2)spring的配置:
- <!-- 包扫描、注解相关 -->
- <context:component-scan base-package="com.inspur.eyun.yunbx">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- <context:exclude-filter type="assignable" expression="com.inspur.eyun.yunbx.swagger.SwaggerConfig"/>
- </context:component-scan>
注意把swagger排除掉
(3)Swagger的配置:
- @Configuration
- @EnableSwagger2
- @EnableWebMvc
- @ComponentScan("com.inspur.eyun.yunbx.controller")
- public class SwaggerConfig{
- }
注意@Configuration注解。
当然更推荐的办法是使用xml配置的方式,因为这样可以不用引入swagger的依赖包:
(1)spring-mvc的配置:
- <!-- 使用Annotation自动注册Bean,只扫描@Controller -->
- <context:component-scan base-package="com.inspur.eyun.yunbx" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
- <import resource="classpath:spring-mvc-swagger.xml" />
spring-mvc-swagger.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd">
- <description>SpringMVC Swagger Configuration</description>
- <!-- swagger配置,生产环境置空 -->
- <bean class="com.inspur.eyun.yunbx.swagger.SwaggerConfig" />
- </beans>
注意:我们这里把swagger单独放到一个配置文件中,如果是线上环境,则文件内容为空,如果是线下测试环境,则配置上Swagger。
(2)spring的配置:
- <!-- 包扫描、注解相关 -->
- <context:component-scan base-package="com.inspur.eyun.yunbx">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
(3)Swagger的配置:
- @EnableSwagger2
- @EnableWebMvc
- public class SwaggerConfig{
- @Bean
- public Docket customDocket() {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.inspur.eyun.yunbx.controller"))
- .paths(PathSelectors.any())
- .build();
- }
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("XXX平台")
- .description("XXX平台接口")
- .license("")
- .licenseUrl("")
- .termsOfServiceUrl("")
- .version("1.0.0")
- .contact(new Contact("","", ""))
- .build();
- }
- }
注意:这里我们去掉了@Configuration,同时,修改我们的pom,配置多profile打包:
pom.xml:
- <!-- Swagger -->
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>${springfox-swagger.version}</version>
- <scope>${swagger.scope}</scope>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <scope>${swagger.scope}</scope>
- <version>${springfox-swagger-ui.version}</version>
- </dependency>
注意:这里的依赖的scope是动态设置的,如果是线上环境,我们把scope设置成provided就可以。
- <profiles>
- <profile>
- <id>dev</id>
- <properties>
- <profiles.active>dev</profiles.active>
- <swagger.scope>compile</swagger.scope>
- </properties>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <profile>
- <id>test</id>
- <properties>
- <profiles.active>test</profiles.active>
- <swagger.scope>compile</swagger.scope>
- </properties>
- </profile>
- <profile>
- <id>online</id>
- <properties>
- <profiles.active>online</profiles.active>
- <swagger.scope>provided</swagger.scope>
- </properties>
- </profile>
- </profiles>
通过不同的profile给swagger的依赖设置不同的scope!
注意:springfox-swagger.version=2.7.0有bug,可以使用低版本2.6.1。太他妈的坑!
如何在生产环境禁用swagger的更多相关文章
- 配置不同环境下启用swagger,在生产环境关闭swagger
前言 Swagger使用起来简单方便,几乎所有的API接口文档都采用swagger了.使用示例:http://www.cnblogs.com/woshimrf/p/swagger.html, 现在开发 ...
- 生产环境屏蔽swagger(动态组装bean)
spring动态组装bean 背景介绍: 整合swagger时需要在生产环境中屏蔽掉swagger的地址,不能在生产环境使用 解决方案 使用动态profile在生产环境中不注入swagger的bean ...
- 在生产环境下禁用swagger
学习目标 快速学会使用注解关闭Swagger2,避免接口重复暴露. 使用教程 禁用方法1:使用注解@Profile({"dev","test"}) 表示在开发或 ...
- SpringBoot 通过配置禁用swagger
转自:https://blog.csdn.net/weixin_37264997/article/details/82762050 一.序言 在生产环境下,我们需要关闭swagger配置,避免暴露接口 ...
- 【简记】SpringBoot禁用Swagger
楔子 Swagger 是 Java Web 开发中常用的接口文档生成类库,在开发和前后端联调时使用它来模拟接口调用能提高开发效率.但是,在生产环境可能并不需要它,一个原因是启用它会延长程序启动时间(动 ...
- Greenplum 数据库安装部署(生产环境)
Greenplum 数据库安装部署(生产环境) 硬件配置: 16 台 IBM X3650, 节点配置:CPU 2 * 8core,内存 128GB,硬盘 16 * 900GB,万兆网卡. 万兆交换机. ...
- [译]MongoDb生产环境注意事项
译注: 本文是翻译MongoDB Manuel中的MongoDB Production Notes一节内容.这节内容重点关注生产环境中影响性能和可靠性的各种注意事项,值得正在部署MongoDB的工作者 ...
- 【转】生产环境MySQL Server核心参数的配置
⑴ lower_case_table_names ● 推荐理由 GNU/Linux 平台,对数据库.表.存储过程等对象名称大小 ...
- MongoDB 生产环境笔记
目录 MongoDB 生产环境笔记 一.vm.zone_reclaim_mode 参数 二.添加 swap 分区 三.设置 swappiness 参数 四.内核和文件系统版本 五.禁用 Transpa ...
随机推荐
- 限制用户不能删除SharePoint列表中的条目(项目)
概述 SharePoint列表提供了一个用于在线协作的电子表格,不同的用户可以同时在不同的地方编辑一套数据. 列表功能在收集用户信息.提供审批流程方面为办公人员提供了非常便捷的好处. 既然是协作办公, ...
- 如何将Jenkins multiline string parameter的多行文本优雅的保存为文件
[现象]: 使用multi-line string parameter获取的文本变量,在jenkins shell里面显示为单行文本(空格分割). [问题]:能否转换为多行文本,并存入文件. [解决方 ...
- ASP.NET网页VS利用文件系统发布
1.点击发布 2.选择发布方式,这里选择文件系统,并选择发布的路径 3.配置相关参数 4.点击发布按钮 5.发布成功后文件夹下生成的文件 ..
- Ubuntu安装Apache+PHP
1.Ubuntu 安装Apache 安装Apache sudo apt-get install apache2 修改端口号 打开 etc/apache2/port.config 修改 port 80 ...
- 【mysql】Windows环境搭建(适用5.7以上)
1 下载MySQL 登录 https://dev.mysql.com/downloads/mysql/ 2 配置 下载好了zip文件,解压至任意非中文目录,在根目录下新建my.ini: 输入以下内容( ...
- WinForm ListView不分页加载大量数据
WinForm的ListView在加载大量数据时会出现闪烁的问题,同时数据加载很慢.如果你的列表中有超过千条的数据且不做特殊处理还是用普通的ListView.Items.Add(),估计你的用户得抱怨 ...
- 纯文本-FileInputStream的编码与解码方式
前言:以下分析只针对纯文本 1.FileInputStream默认的编码方式就是文件的编码方式 即:源文件是什么编码方式,则利用FileInputStream默认读取的字节数组,就是什么编码方式. 例 ...
- 用layui遇到过的问题
1.报错“layui.form is not a function”问题 把代码中这一串修改一下:form = layui.form(); 括号去掉就行: form = layui.form; 如果你 ...
- docker 运行容器,安装Nginx
########################################## #运行容器 #安装Nginx #搜索.下载镜像 docker search nginx docker pull n ...
- php主要用于哪几方面
1,服务端脚本,网站和web应用程序,web服务器,php解析器,web浏览器 2,命令行脚本 3,编写桌面应用程序