golang gin框架 使用swagger生成api文档

golang gin框架 使用swagger生成api文档,第1张

概述github地址:https://github.com/swaggo/gin-swagger 1、下载swag $ go get -u github.com/swaggo/swag/cmd/swag 2、在main.go所在目录执行 $ swag init 生成docs/doc.go以及docs/swagger.json,docs/swagger.yaml 3、下载gin-swagger $ go

github地址:https://github.com/swaggo/gin-swagger

1、下载swag

$ go get -u github.com/swaggo/swag/cmd/swag

2、在main.go所在目录执行

$ swag init

生成docs/doc.go以及docs/swagger.Json,docs/swagger.yaml

3、下载gin-swagger

$ go get -u github.com/swaggo/gin-swagger$ go get -u github.com/swaggo/files

然后在路由文件引入

import (	"github.com/gin-gonic/gin" swaggerfiles "github.com/swaggo/files" "github.com/swaggo/gin-swagger" _ "github.com/swaggo/gin-swagger/example/basic/docs" // docs is generated by Swag Cli,you have to import it. )

并增加swagger访问路由

url := ginSwagger.URL("http://localhost:8080/swagger/doc.Json") // The url pointing to API deFinition    r.GET("/swagger/*any",ginSwagger.WrapHandler(swaggerfiles.Handler,url))

 

3、一些注解,编写各API handler方法注释(注解格式传送门)

1)main.go主程序文件注释:

// @Title Golang Esign API// @version 1.0// @description  Golang API of demo// @termsOfService http://github.com// @contact.name API Support// @contact.url http://www.cnblogs.com// @contact.email ×××@qq.com//@host 127.0.0.1:8081func main() {}

2)handler方法注释:eg

//CreatScene createScene// @Summary createScene// @Description createScene// @Accept multipart/form-data// @Produce  Json// @Param app_key formData string true "AppKey"// @Param nonce_str formData string true "NonceStr"// @Param time_stamp formData string true "TimeStamp"// @Success 200 {object} app.R// @Failure 500 {object} app.R// @Router /dictionarIEs/createScene [post]
总结

以上是内存溢出为你收集整理的golang gin框架 使用swagger生成api文档全部内容,希望文章能够帮你解决golang gin框架 使用swagger生成api文档所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1264969.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存