

【笔记】Go将Gin整合Swagger自动生成API文档
source link: https://loli.fj.cn/2023/04/18/Go%E5%B0%86Gin%E6%95%B4%E5%90%88Swagger%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90API%E6%96%87%E6%A1%A3/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Go将Gin整合Swagger自动生成API文档学习笔记
下载可执行文件
go install github.com/swaggo/swag/cmd/[email protected]
在项目中下载依赖
go get github.com/swaggo/gin-swagger
go get github.com/swaggo/files
在Gin的路由中添加Swagger的路由
import (
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
func main() {
app := gin.Default()
app.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
app.Run()
}
在函数前添加Swagger语法的注释
- 在主函数上添加全局API文档注释
- 在接口函数上添加接口API文档注释
_ "demo/docs"
:指定swag init
生成的文档目录的相对路径作为包引入
在主函数上添加全局API文档注释
@title
:标题@version
:版本号@description
:文档描述@contact.name
:作者名@contact.email
:作者邮箱@host
:访问地址@basePath
:项目访问路径在接口函数上添加接口API文档注释
@tag
:标签@summary
:接口标题@description
:接口描述@param
:接口参数第1个参数:参数变量名
第2个参数
query
:请求头携带的参数body
:请求体携带的参数第3个参数:数据类型
第4个参数:是否是必须,true必须,false不必须
第5个参数:参数描述
@router /index [get]
:路由第1个参数:请求URL
第2个参数:请求类型
@produce
:请求数据格式@success 200
:成功处理的响应
demo/main.go
import (
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
_ "demo/docs"
)
type Request struct {
Key string `json:"key"`
}
type Response struct {
Code string `json:"code"`
Msg string `json:"data"`
}
// @title xxx
// @version v1.0
// @description xxx
// @contact.name xxx
// @contact.email [email protected]
// @host 127.0.0.1:8080
// @basePath /
func main() {
app := gin.Default()
app.GET("/index", Index)
app.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
app.Run()
}
// Index 首页
// @tag xxx模块
// @summary xxx功能
// @description xxx
// @param xxx query Request false "xxx"
// @param xxx body string true "xxx"
// @router /index [get]
// @produce json
// @success 200 {object} Response
func Index(context *gin.Context) {
context.JSON(200, Response{0, "ok"})
}
- 每次改动Swagger语法的注释后都需要重新生成文档
swag init
- 会在当前位置自动创建
docs
目录,并存放生成后的文档
- 启动服务,浏览器访问http://127.0.0.1:8080/swagger/index.html
Recommend
-
32
Golang 自动生成swagger 安装 go get -u github.com/swaggo/swag/cmd/swag 在项目下执行 swag init ,会生成docs目录。如果目录存在则会报错。 ...
-
8
如何利用showdoc自动生成API文档 showdoc是一个适合IT团队的文档工具,阅读本文前需要对showdoc有基本了解 。基本介绍可看:http...
-
7
V2EX › 程序员 大家现在还用 swagger 生成 API 文档吗 shadowfish0 · 5 小时 35 分...
-
6
从Python源码注释,自动生成API文档用python写了一个
-
1
本文转载自: https://razeencheng.com/post/go-swagger.html 相信很多程序猿和我一样不喜欢写API文档。写代码多舒服,写文档不仅要花费大量的时间,有时候还不能做到面面具全。但API文档是...
-
9
什么是swagger?Swagger是一个简单但功能强大的API表达工具。它具有地球上最大的API工具生态系统,数以千计的开发人员,使用几乎所有的现代编程语言,都在支持和使用Swagger。使用...
-
5
Gin 配置 Swagger 接口文档 返回 此包用于自动化生成 API 文档。 下载 swag 工具: $ go get -u gi...
-
9
Spring Boot整合Swagger时PatternsRequestCondition.getPatterns()报错:"this.condition" is null
-
8
微服务系列之Api文档 swagger整合 1.前言 微服...
-
11
为Golang API设置Swagger文档 追梦人在路上不断追寻 · 2020-10-16 00:33:08 · 1365 次点击 · 预计阅读时间 2 分钟 · 大约8小时之前 开始浏览 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK