Echo Swagger使用


Echo Swagger使用

1.源碼編譯

 go get github.com/swaggo/swag/cmd/swag
 
 cd github.com/swaggo/swag/cmd
 go build .  //生成swag二進制文件,可拷貝到/usr/local/go/bin目錄,便於使用

2.或者直接下載二進制文件
https://github.com/swaggo/swag/releases //解壓后,將swag文件拷貝到Path路徑中

3.controller中加入對應的注解

// @Summary Book
// @Description get book info
// @Tags book
// @Accept json
// @Produce json
// @Success 200 {string} string "Success"
// @Failure 400 {string} string "Failed"
// @Failure 404 {string} string "Not Found"
// @Failure 500 {string} string "Internal Error"
// @Router /xxx/shosacts [get]

標簽說明

4.進入工程目錄,執行如下命令

swag init  //自動生成doc目錄

Goland 安裝swag插件,可以實時預覽!!!

5.main.go中加入swagger中間件
(1.)導包

import	"github.com/swaggo/echo-swagger"
import	_ "xxx/自己項目的目錄/docs"  //該目錄為swag init生成的doc目錄

(2.)main函數中加入swagger API相關的信息

// @title Swagger Book API
// @version 1.0
// @description This is a echo swaagger api doc.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host petstore.swagger.io
// @BasePath /v2
e.GET("/swagger/*", echoSwagger.WrapHandler)  //echo-swagger中間件

(3.)編譯運行,訪問頁面

go build .
./xxx   //運行二進制文件

http://localhost:port/swagger/index.html

6.注釋說明:
https://www.bookstack.cn/read/topgoer/954f42d1c9b9ff8a.md


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM