gin 生成api文档_golang gin框架 集成swagger 自动生成文档
goswagger github倉庫html
https://github.com/swaggo/swag
安裝 swag cligit
1.由于網絡緣由,先安裝gopm 管理工具github
go get -v -u github.com/gpmgo/gopm
安裝到了 $GOPTH/bin里 找不到的話,用 sudo find / -name gopm 找一下golang
2.安裝swagweb
gopm get -g -v github.com/swaggo/swag/cmd/swag
過程當中可能會報錯,重試便可json
3.找到 swag ( find / -name swag ====》? ?cd /go/src/github.com/swaggo/swag/cmd/swag/) 會看到main.go 文件api
4 執行:go install (可能會報錯缺乏包,挨個安裝 go get -u ?github.com/urfave/cli)網絡
5 再次執行 ?go install?函數
不出意外的話,swag安裝成功了工具
找到swag執行文件,應該在$GOPTH/bin里面,而后加成全局變量(?sudo cp swag /usr/local/go/bin/)
而后試一下swag -version
lhs:redigo houlv$ swag -version
swag version v1.6.2
成功!
在golang-gin項目上集成swagger
找到main函數所在的類
添加如下代碼
import里
swaggerFiles "github.com/swaggo/files"ginSwagger"github.com/swaggo/gin-swagger"_"testsu.cn/rocket/docs"
其中docs是你生成docs的路徑
面函數里實現
g.Go(func() error {
r :=gin.New()
url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") //The url pointing to API definition
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
r.Run()returnnil
})
go.mod里添加
github.com/swaggo/gin-swagger v1.2.1-0.20190717074206-d488a692749f
github.com/swaggo/swag v1.5.1
在API handler文件里添加標準注解
例如:
//@Summary 生成pdf文檔//@Description 傳遞id生成pdf文檔//@Accept json//@Param id path int true "id"//@Success 200 {object} model.VersionDTO//@Failure 500 {string} json "{"code":500,"data":{},"msg":"ok"}"//@Router /report/{id}/pdf/download [get]
func (handler *TestcaseReportHtml) DownloadTestCasePDF(c *gin.Context) {
而后在項目的根目錄執行
swag init -g launcher/api-rocket/web.go
-g 后面所跟的為main方法在的位置
便可看到自動生成的文檔
入截圖所示:
最后添加:將swagger的標準庫添加上:
標準注釋
https://swaggo.github.io/swaggo.io/declarative_comments_format/general_api_info.html
原文出處:https://www.cnblogs.com/tsxylhs/p/11436522.html
總結
以上是生活随笔為你收集整理的gin 生成api文档_golang gin框架 集成swagger 自动生成文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: element的滚动去掉横向_texta
- 下一篇: datatable 创建列赋值_Data