pringMVC集成springfox-swagger2和springfox-swagger-ui很简单,只需要两步:

(1)pom中添加依赖

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger-ui</artifactId>
  4. <version>${springfox-swagger.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger2</artifactId>
  9. <version>${springfox-swagger.version}</version>
  10. </dependency>

(2)添加Swagger的配置类:

  1. @Configuration
  2. @EnableSwagger2
  3. @EnableWebMvc
  4. @ComponentScan("com.XXX.controller")
  5. public class SwaggerConfig{
  6. }

然后就可以通过http://localhost/swagger-ui.html看到项目中所有的接口信息了,通过http://localhost/v2/api-docs就能看到json数据。

转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/78280051

但是,如何在生产环境禁用这些api文档呢?试了很多种方式,最终找到一个简单实用的办法:

  1. @Configuration
  2. @EnableSwagger2
  3. @EnableWebMvc
  4. @ComponentScan("com.XXX.controller")
  5. public class SwaggerConfig{
  6. @Autowired
  7. ConfigService configService;
  8. @Bean
  9. public Docket customDocket() {
  10. if(configService.getServerEnv() == ServerEnvEnum.ONLINE) {
  11. return new Docket(DocumentationType.SWAGGER_2)
  12. .apiInfo(apiInfoOnline())
  13. .select()
  14. .paths(PathSelectors.none())//如果是线上环境,添加路径过滤,设置为全部都不符合
  15. .build();
  16. }else {
  17. return new Docket(DocumentationType.SWAGGER_2)
  18. .apiInfo(apiInfo());
  19. }
  20. }
  21. private ApiInfo apiInfo() {
  22. return new ApiInfoBuilder()
  23. .title("XXX系统")
  24. .description("XXX系统接口")
  25. .license("")
  26. .licenseUrl("")
  27. .termsOfServiceUrl("")
  28. .version("1.0.0")
  29. .contact(new Contact("","", ""))
  30. .build();
  31. }
  32. private ApiInfo apiInfoOnline() {
  33. return new ApiInfoBuilder()
  34. .title("")
  35. .description("")
  36. .license("")
  37. .licenseUrl("")
  38. .termsOfServiceUrl("")
  39. .version("")
  40. .contact(new Contact("","", ""))
  41. .build();
  42. }
  43. }
  44. 参考:http://blog.csdn.net/w4hechuan2009/article/details/68892718

    swagger必须要跟springmvc在同一个context才行,springmvc只是spring的一个子context。如果swagger让spring的context加载,那么swagger的那些url用springmvc的拦截器是拦截不到的!

    所以,两种解决办法:

    如果是使用注解的方式:

    (1)spring-mvc的配置:

    1. <!-- 使用Annotation自动注册Bean,只扫描@Controller -->
    2. <context:component-scan base-package="com.inspur.eyun.yunbx" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
    3. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    4. <context:include-filter type="assignable" expression="com.inspur.eyun.yunbx.swagger.SwaggerConfig"/>
    5. </context:component-scan>

    注意要把swagger的配置加进来,同时:

    (2)spring的配置:

    1. <!-- 包扫描、注解相关 -->
    2. <context:component-scan base-package="com.inspur.eyun.yunbx">
    3. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    4. <context:exclude-filter type="assignable" expression="com.inspur.eyun.yunbx.swagger.SwaggerConfig"/>
    5. </context:component-scan>

    注意把swagger排除掉

    (3)Swagger的配置:

    1. @Configuration
    2. @EnableSwagger2
    3. @EnableWebMvc
    4. @ComponentScan("com.inspur.eyun.yunbx.controller")
    5. public class SwaggerConfig{
    6. }

    注意@Configuration注解。

    当然更推荐的办法是使用xml配置的方式,因为这样可以不用引入swagger的依赖包:

    (1)spring-mvc的配置:

    1. <!-- 使用Annotation自动注册Bean,只扫描@Controller -->
    2. <context:component-scan base-package="com.inspur.eyun.yunbx" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
    3. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    4. </context:component-scan>
    5. <import resource="classpath:spring-mvc-swagger.xml" />

    spring-mvc-swagger.xml:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="
    5. http://www.springframework.org/schema/beans
    6. http://www.springframework.org/schema/beans/spring-beans.xsd">
    7. <description>SpringMVC Swagger Configuration</description>
    8. <!-- swagger配置,生产环境置空 -->
    9. <bean class="com.inspur.eyun.yunbx.swagger.SwaggerConfig" />
    10. </beans>

    注意:我们这里把swagger单独放到一个配置文件中,如果是线上环境,则文件内容为空,如果是线下测试环境,则配置上Swagger。

    (2)spring的配置:

    1. <!-- 包扫描、注解相关 -->
    2. <context:component-scan base-package="com.inspur.eyun.yunbx">
    3. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    4. </context:component-scan>

    (3)Swagger的配置:

    1. @EnableSwagger2
    2. @EnableWebMvc
    3. public class SwaggerConfig{
    4. @Bean
    5. public Docket customDocket() {
    6. return new Docket(DocumentationType.SWAGGER_2)
    7. .apiInfo(apiInfo())
    8. .select()
    9. .apis(RequestHandlerSelectors.basePackage("com.inspur.eyun.yunbx.controller"))
    10. .paths(PathSelectors.any())
    11. .build();
    12. }
    13. private ApiInfo apiInfo() {
    14. return new ApiInfoBuilder()
    15. .title("XXX平台")
    16. .description("XXX平台接口")
    17. .license("")
    18. .licenseUrl("")
    19. .termsOfServiceUrl("")
    20. .version("1.0.0")
    21. .contact(new Contact("","", ""))
    22. .build();
    23. }
    24. }

    注意:这里我们去掉了@Configuration,同时,修改我们的pom,配置多profile打包:

    pom.xml:

    1. <!-- Swagger -->
    2. <dependency>
    3. <groupId>io.springfox</groupId>
    4. <artifactId>springfox-swagger2</artifactId>
    5. <version>${springfox-swagger.version}</version>
    6. <scope>${swagger.scope}</scope>
    7. </dependency>
    8. <dependency>
    9. <groupId>io.springfox</groupId>
    10. <artifactId>springfox-swagger-ui</artifactId>
    11. <scope>${swagger.scope}</scope>
    12. <version>${springfox-swagger-ui.version}</version>
    13. </dependency>

    注意:这里的依赖的scope是动态设置的,如果是线上环境,我们把scope设置成provided就可以。

    1. <profiles>
    2. <profile>
    3. <id>dev</id>
    4. <properties>
    5. <profiles.active>dev</profiles.active>
    6. <swagger.scope>compile</swagger.scope>
    7. </properties>
    8. <activation>
    9. <activeByDefault>true</activeByDefault>
    10. </activation>
    11. </profile>
    12. <profile>
    13. <id>test</id>
    14. <properties>
    15. <profiles.active>test</profiles.active>
    16. <swagger.scope>compile</swagger.scope>
    17. </properties>
    18. </profile>
    19. <profile>
    20. <id>online</id>
    21. <properties>
    22. <profiles.active>online</profiles.active>
    23. <swagger.scope>provided</swagger.scope>
    24. </properties>
    25. </profile>
    26. </profiles>

    通过不同的profile给swagger的依赖设置不同的scope!

    注意:springfox-swagger.version=2.7.0有bug,可以使用低版本2.6.1。太他妈的坑!

如何在生产环境禁用swagger的更多相关文章

  1. 配置不同环境下启用swagger,在生产环境关闭swagger

    前言 Swagger使用起来简单方便,几乎所有的API接口文档都采用swagger了.使用示例:http://www.cnblogs.com/woshimrf/p/swagger.html, 现在开发 ...

  2. 生产环境屏蔽swagger(动态组装bean)

    spring动态组装bean 背景介绍: 整合swagger时需要在生产环境中屏蔽掉swagger的地址,不能在生产环境使用 解决方案 使用动态profile在生产环境中不注入swagger的bean ...

  3. 在生产环境下禁用swagger

    学习目标 快速学会使用注解关闭Swagger2,避免接口重复暴露. 使用教程 禁用方法1:使用注解@Profile({"dev","test"}) 表示在开发或 ...

  4. SpringBoot 通过配置禁用swagger

    转自:https://blog.csdn.net/weixin_37264997/article/details/82762050 一.序言 在生产环境下,我们需要关闭swagger配置,避免暴露接口 ...

  5. 【简记】SpringBoot禁用Swagger

    楔子 Swagger 是 Java Web 开发中常用的接口文档生成类库,在开发和前后端联调时使用它来模拟接口调用能提高开发效率.但是,在生产环境可能并不需要它,一个原因是启用它会延长程序启动时间(动 ...

  6. Greenplum 数据库安装部署(生产环境)

    Greenplum 数据库安装部署(生产环境) 硬件配置: 16 台 IBM X3650, 节点配置:CPU 2 * 8core,内存 128GB,硬盘 16 * 900GB,万兆网卡. 万兆交换机. ...

  7. [译]MongoDb生产环境注意事项

    译注: 本文是翻译MongoDB Manuel中的MongoDB Production Notes一节内容.这节内容重点关注生产环境中影响性能和可靠性的各种注意事项,值得正在部署MongoDB的工作者 ...

  8. 【转】生产环境MySQL Server核心参数的配置

         ⑴ lower_case_table_names              ● 推荐理由                    GNU/Linux 平台,对数据库.表.存储过程等对象名称大小 ...

  9. MongoDB 生产环境笔记

    目录 MongoDB 生产环境笔记 一.vm.zone_reclaim_mode 参数 二.添加 swap 分区 三.设置 swappiness 参数 四.内核和文件系统版本 五.禁用 Transpa ...

随机推荐

  1. leetcode 123. 买卖股票的最佳时机 III JAVA

    题目: 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与多笔交易(你必须在再次购买前出 ...

  2. 极大提高Web开发效率的8个工具和建议(含教程)

    面对复杂的 Web 应用的开发,良好的流程和工具支持是必不可少的,它们可以让日常的开发工作更加顺畅.更加高效.本文介绍了6个Web开发利器以及相关的教程,帮助你在开发.调试.集成和发布过程极大地提高效 ...

  3. MySQL数据库命令大全

    --数据库操作前的准备-- 创建数据库-- create database python_test_1 charset=utf8; -- 使用数据库-- use python_test_1; -- s ...

  4. 查询改写(Query Rewrite)方法总结

    为何需要Query改写 Query分析是搜索引擎的一个重要模块,对搜索结果的覆盖率和相关性至关重要.搜索引擎的检索过程包含了两个重要的阶段:匹配和排序.匹配也叫召回,表示根据用户的查询条件,尽可能多地 ...

  5. Process Explore & Windbg

    遇到点内存泄漏.句柄泄漏,应该是家常便饭了.这次就是,程序运行内存一点点增加,句柄也是只增不减,个数竟然可以达到几十万,真是瞪大了我的双眼. 借此机会,学习下相关工具~ Process Explore ...

  6. 通过sessionStorage来根据屏幕宽度变化来加载不同的html页面

    因为项目需要,分别写了移动端和PC端的两个html页面,现在需要根据不同的屏幕宽度来加载对应的页面. 先说一下本人的思路-- 刚开始我直接在加载页面的时候判断屏幕宽度,然后加载相应的页面,大家是不是也 ...

  7. 腾讯云 利用php + apache + mysql 搭建服务器环境

    1.一键安装需要的软件源 yum install -y httpd php php-fpm mysql mysql-server php-mysql 1) httpd 即为 apache 2)php  ...

  8. js之作用域

    1.什么是作用域 作用域是用于收集存储维护变量,以及当前执行代码声明的变量所拥有的权限, 例如 : function foo(a){ console.log(a); --------    1    ...

  9. Kubernetes使用GlusterFS实现数据持久化

    k8s中部署有状态应用等需要持久化数据的应用,必不可少得用存储,k8s支持很多中存储方案,我司目前使用的存储有glusterfs(分为容器化和裸机方式).nfs供应用选用,本次就简单实战下gluste ...

  10. 转 ZFC公理系统

    http://blog.sina.com.cn/s/blog_5d045b5c0100spld.html 首先,ZFC集合论中的公理大致分为3组: 1.外延公理. 2.子集公理模式.无序对公理.并集公 ...