Echo Swagger使用

本文介绍如何使用EchoSwagger为Go项目生成API文档,包括源码编译、控制器注解、自动生成doc目录、安装及配置Goland插件、集成swagger中间件等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值