打赏

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

posted @ 2020-05-12 13:52  苍山落暮  阅读(1509)  评论(0编辑  收藏  举报