SpringMVC如何在生產(chǎn)環(huán)境禁用Swagger的方法
Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)。總體目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來(lái)更新。文件的方法,參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許API來(lái)始終保持同步。
Swagger 讓部署管理和使用功能強(qiáng)大的API從未如此簡(jiǎn)單。好吧,以上是官方的說(shuō)法,我直接復(fù)制的,在我看來(lái)swagger就是一個(gè)接口文檔管理器,以前我們寫(xiě)接口一般都是world編寫(xiě),但是有一個(gè)問(wèn)題就是測(cè)試的時(shí)候需要依賴第三方工具,GET的接口還好,直接瀏覽器打開(kāi),POST的只能依賴另外的工具了,而Swagger呢,可以直接通過(guò)代碼中的注解生成接口文檔(JavaEE),一般人都用這種方式,而且直接集成在項(xiàng)目中,方便成員查看,同時(shí)還能直接測(cè)試,另外Swagger的界面也不錯(cuò),也許這就是我選擇用Swagger的原因吧,直接官方說(shuō)的RESTful 風(fēng)格那個(gè)不用管,不是RESTful 風(fēng)格的接口也能用,當(dāng)然Swagger還有一種方式就是手動(dòng)寫(xiě)接口說(shuō)明了,這樣的好處就是代碼只有代碼,因?yàn)橐坏┐a中添加了Swagger的接口注解后,代碼量還是增加了不少,當(dāng)然壞處就是你改完了代碼,還要去改接口文檔
SpringMVC集成springfox-swagger2和springfox-swagger-ui很簡(jiǎn)單,只需要兩步:
(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{
}
然后就可以通過(guò)http://localhost/swagger-ui.html看到項(xiàng)目中所有的接口信息了,通過(guò)http://localhost/v2/api-docs就能看到j(luò)son數(shù)據(jù)。
但是,如何在生產(chǎn)環(huán)境禁用這些api文檔呢?試了很多種方式,最終找到一個(gè)簡(jiǎn)單實(shí)用的辦法:
@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())//如果是線上環(huán)境,添加路徑過(guò)濾,設(shè)置為全部都不符合
.build();
}else {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XXX系統(tǒng)")
.description("XXX系統(tǒng)接口")
.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();
}
}
現(xiàn)在http://localhost/swagger-ui.html這個(gè)頁(yè)面雖然還能訪問(wèn),那是卻看不到任何內(nèi)容了,包括http://localhost/v2/api-docs也是一樣。
應(yīng)該還有更好的辦法!
參考:http://m.fzitv.net/article/135312.htm
swagger必須要跟springmvc在同一個(gè)context才行,springmvc只是spring的一個(gè)子context。如果swagger讓spring的context加載,那么swagger的那些url用springmvc的攔截器是攔截不到的!
所以,兩種解決辦法:
如果是使用注解的方式:
(1)spring-mvc的配置:
<!-- 使用Annotation自動(dòng)注冊(cè)Bean,只掃描@Controller --> <context:component-scan base-package="com.inspur.eyun.yunbx" use-default-filters="false"><!-- base-package 如果多個(gè),用“,”分隔 --> <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的配置加進(jìn)來(lái),同時(shí):
(2)spring的配置:
<!-- 包掃描、注解相關(guān) --> <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注解。
當(dāng)然更推薦的辦法是使用xml配置的方式,因?yàn)檫@樣可以不用引入swagger的依賴包:
(1)spring-mvc的配置:
<!-- 使用Annotation自動(dòng)注冊(cè)Bean,只掃描@Controller --> <context:component-scan base-package="com.inspur.eyun.yunbx" use-default-filters="false"><!-- base-package 如果多個(gè),用“,”分隔 --> <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配置,生產(chǎn)環(huán)境置空 --> <bean class="com.inspur.eyun.yunbx.swagger.SwaggerConfig" /> </beans>
注意:我們這里把swagger單獨(dú)放到一個(gè)配置文件中,如果是線上環(huán)境,則文件內(nèi)容為空,如果是線下測(cè)試環(huán)境,則配置上Swagger。
(2)spring的配置:
<!-- 包掃描、注解相關(guān) --> <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平臺(tái)")
.description("XXX平臺(tái)接口")
.license("")
.licenseUrl("")
.termsOfServiceUrl("")
.version("1.0.0")
.contact(new Contact("","", ""))
.build();
}
}
注意:這里我們?nèi)サ袅薂Configuration,同時(shí),修改我們的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是動(dòng)態(tài)設(shè)置的,如果是線上環(huán)境,我們把scope設(shè)置成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>
通過(guò)不同的profile給swagger的依賴設(shè)置不同的scope!
注意:springfox-swagger.version=2.7.0有bug,可以使用低版本2.6.1。太他媽的坑!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springMVC利用FastJson接口返回json數(shù)據(jù)相關(guān)配置詳解
- springmvc fastjson 反序列化時(shí)間格式化方法(推薦)
- 詳解在springmvc中解決FastJson循環(huán)引用的問(wèn)題
- Spring MVC集成springfox-swagger2構(gòu)建restful API的方法詳解
- SpringMVC集成Swagger實(shí)例代碼
- Spring MVC利用Swagger2如何構(gòu)建動(dòng)態(tài)RESTful API詳解
- SpringMVC和Swagger整合方法
- SpringMVC 中配置 Swagger 插件的教程(分享)
- Spring MVC+FastJson+Swagger集成的完整實(shí)例教程
相關(guān)文章
解決Spring配置文件中bean的property屬性中的name出錯(cuò)問(wèn)題
這篇文章主要介紹了解決Spring配置文件中bean的property屬性中的name出錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
JavaEE中關(guān)于ServletConfig的小結(jié)
ServletConfig是針對(duì)特定的Servlet的參數(shù)或?qū)傩浴ervletConfig是表示單獨(dú)的Servlet的配置和參數(shù),只是適用于特定的Servlet。從一個(gè)servlet被實(shí)例化后,對(duì)任何客戶端在任何時(shí)候訪問(wèn)有效,但僅對(duì)本servlet有效,一個(gè)servlet的ServletConfig對(duì)象不能被另一個(gè)servlet訪問(wèn)2014-10-10
一種類似JAVA線程池的C++線程池實(shí)現(xiàn)方法
線程池(thread pool)是一種線程使用模式。線程過(guò)多或者頻繁創(chuàng)建和銷毀線程會(huì)帶來(lái)調(diào)度開(kāi)銷,進(jìn)而影響緩存局部性和整體性能。這篇文章主要介紹了一種類似JAVA線程池的C++線程池實(shí)現(xiàn)方法,需要的朋友可以參考下2019-07-07
Java使用WeakHashMap實(shí)現(xiàn)緩存自動(dòng)清理
在 Java 中,內(nèi)存管理是一個(gè)重要的話題,尤其是在涉及到緩存的實(shí)現(xiàn)時(shí),如果緩存項(xiàng)不再被使用,我們希望它們能被自動(dòng)清理,而不必手動(dòng)刪除,WeakHashMap 就是 Java 提供的一種用于緩存和內(nèi)存管理的工具,本文將深入探討如何利用 WeakHashMap 來(lái)實(shí)現(xiàn)緩存自動(dòng)清理2025-01-01
idea springboot遠(yuǎn)程debug的操作方法
這篇文章主要介紹了idea springboot遠(yuǎn)程debug的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

