參考文檔
swagger ui 源碼地址 https://github.com/swagger-api/swagger-ui ,swagger ui由nodejs開發,可以根據項目要求讓前端人員進行二次開發
swagger dubbo集成參考文檔:https://github.com/Sayi/swagger-dubbo
swagger dubbo引入mock 參考文檔:https://blog.csdn.net/gloomy_114/article/details/73500818
Swagger圍繞着OpenAPI規范,提供了一套設計、構建、文檔化rest api的開源工具。
swagger-dubbo主要作用是swagger式的文檔化+rest風格的HTTP模擬測試。
- 通過swagger閱讀接口文檔
- 開發人員可以用它來自測服務接口,也可以用它來模擬別人的服務接口返回值
- 測試可以用它來驗證接口的正確性,基於HTTP進行接口測試
swagger-dubbo從某些方面提高了內部開發測試的效率,注意的是,rest服務不適合對外(前端)提供,務必在服務端或者測試內部使用
Maven
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>swagger-dubbo</artifactId>
<version>
2.0
.
1
</version>
</dependency>
swagger集成
一. 使用注解 @EnableDubboSwagger
開啟dubbo的swagger文檔。
package
com.deepoove.swagger.dubbo.example;
import
org.springframework.context.annotation.Configuration;
import
com.deepoove.swagger.dubbo.annotations.EnableDubboSwagger;
@Configuration
@EnableDubboSwagger
public
class
SwaggerDubboConfig {
}
<mvc:annotation-driven/>
<context:annotation-config />
<bean
class
=
"com.wt.swagger.dubbo.config.SwaggerDubboConfig"
/>
<context:property-placeholder />
<!-- 跨域支持,Spring4.
3.10
+,低版本請設置攔截器開啟跨域 -->
<mvc:cors>
<mvc:mapping path=
"/swagger-dubbo/**"
allowed-origins=
"*"
/>
</mvc:cors>
四.配置項修改
swagger-dubbo默認無需任何配置,但是也提供了一些可選項。我們這里強制大家都需要把配置給添加進去。
如果是沒有接入apollo配置中心,則在新增文件swagger-dubbo.properties,加載配置文件,並且在antx配置文件中添加相應的配置項。
<context:property-placeholder location="classpath*:swagger-dubbo.properties"
/>
#http請求地址,默認為http:
//ip:port/h/com.XXX.XxService/method
swagger.dubbo.http=h
#dubbo 服務版本號
swagger.dubbo.application.version =
1.0
#dubbo服務groupId
swagger.dubbo.application.groupId = com.wt
#dubbo服務artifactId
swagger.dubbo.application.artifactId = payment-service
#rpc zk調用 or 本地調用
swagger.dubbo.cluster = rpc
#是否啟用swagger-dubbo,默認為
true
,如果是生產環境就設置為
false
swagger.dubbo.enable =
true
五.備注
以上配置步驟,都是基於項目中已經引入了springmvc相關依賴為前提,如果項目中沒有則需要自行加入。
集成已經完畢,啟動web容器,瀏覽器訪問 http://ip:port/context/swagger-dubbo/api-docs
查看文檔。
這里已payment為例:http://120.26.205.32:9023/payment/swagger-dubbo/api-docs
swagger-ui查看文檔
swagger-ui訪問地址如下:http://101.37.117.177:8080/static/#/