Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了golang gin框架 使用swagger生成api文档大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_1@

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

1、下载swag

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

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

$ swag init@H_874_9@ 

生成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@H_874_9@ 

然后在路由文件引入

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. )@H_874_9@ 

增加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))@H_874_9@ 

 

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:8081
func main() {
}@H_874_9@ 

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]@H_874_9@ 

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。