加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 09:21:48 所属栏目:大数据 来源:网络整理
导读: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

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:8081
func 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]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读