Echo Swagger使用

时间:2020-05-12
本文章向大家介绍Echo Swagger使用,主要包括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 /contractMgmt/showcontracts [get]

[标签说明](https://github.com/swaggo/swag/blob/master/README_zh-CN.md#%E5%A3%B0%E6%98%8E%E5%BC%8F%E6%B3%A8%E9%87%8A%E6%A0%BC%E5%BC%8F)

4.进入工程目录,执行如下命令

swag init  //自动生成doc目录

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

原文地址:https://www.cnblogs.com/tomtellyou/p/12875748.html