zoukankan      html  css  js  c++  java
  • 如何在生产环境禁用swagger

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

    (1)pom中添加依赖

    [java] view plain copy
     
    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的配置类:

    [java] view plain copy
     
    1. @Configuration   
    2. @EnableSwagger2   
    3. @EnableWebMvc  
    4. @ComponentScan("com.XXX.controller")  
    5. public class SwaggerConfig{  
    6.   
    7. }  

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

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

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

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

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

      所以,两种解决办法:

      如果是使用注解的方式:

      (1)spring-mvc的配置:

      [html] view plain copy
       
      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的配置:

      [html] view plain copy
       
      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的配置:

      [java] view plain copy
       
      1. @Configuration   
      2. @EnableSwagger2   
      3. @EnableWebMvc  
      4. @ComponentScan("com.inspur.eyun.yunbx.controller")  
      5. public class SwaggerConfig{  
      6. }  
      注意@Configuration注解。

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

      (1)spring-mvc的配置:

      [html] view plain copy
       
      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:

      [html] view plain copy
       
      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的配置:

      [html] view plain copy
       
      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的配置:

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

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

      pom.xml:

      [html] view plain copy
       
      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就可以。
      [html] view plain copy
       
      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。太他妈的坑!

  • 相关阅读:
    有效挽回流失用户的数据分析及策略:需多一点套路才能得人心
    有效挽回流失用户的数据分析及策略:需多一点套路才能得人心
    大数据时代管理会计的创新
    大数据时代管理会计的创新
    人们应该如何理性认识大数据
    人们应该如何理性认识大数据
    “忘掉大数据”之“法”:大数据修炼要五讲四美
    “忘掉大数据”之“法”:大数据修炼要五讲四美
    小白学数据分析--留存率使用的窘境
    大数据时代的风险管理
  • 原文地址:https://www.cnblogs.com/cmfwm/p/8022261.html
Copyright © 2011-2022 走看看