SpringMVC如何在生產環境禁用Swagger的方法,springmvcswagger

來源:互聯網
上載者:User

SpringMVC如何在生產環境禁用Swagger的方法,springmvcswagger

Swagger 是一個規範和完整的架構,用於產生、描述、調用和可視化 RESTful 風格的 Web 服務。總體目標是使用戶端和檔案系統作為伺服器以同樣的速度來更新。檔案的方法,參數和模型緊密整合到伺服器端的代碼,允許API來始終保持同步。

Swagger 讓部署管理和使用功能強大的API從未如此簡單。好吧,以上是官方的說法,我直接複製的,在我看來swagger就是一個介面文件管理器,以前我們寫介面一般都是world編寫,但是有一個問題就是測試的時候需要依賴第三方工具,GET的介面還好,直接瀏覽器開啟,POST的只能依賴另外的工具了,而Swagger呢,可以直接通過代碼中的註解產生介面文檔(JavaEE),一般人都用這種方式,而且直接整合在項目中,方便成員查看,同時還能直接測試,另外Swagger的介面也不錯,也許這就是我選擇用Swagger的原因吧,直接官方說的RESTful 風格那個不用管,不是RESTful 風格的介面也能用,當然Swagger還有一種方式就是手動寫介面說明了,這樣的好處就是代碼只有代碼,因為一旦代碼中添加了Swagger的介面註解後,代碼量還是增加了不少,當然壞處就是你改完了代碼,還要去改介面文檔

SpringMVC整合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資料。

但是,如何在生產環境禁用這些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://localhost/swagger-ui.html這個頁面雖然還能訪問,那是卻看不到任何內容了,包括http://localhost/v2/api-docs也是一樣。

應該還有更好的辦法!

參考:http://www.bkjia.com/article/135312.htm

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。太他媽的坑!

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.