Golang – Gin & Swaggo 使用方法

前言

Golang使用swagger 各种请求&参数获取方式的常用注释

开发环境:

  • Windows 10
  • GoLand 2020.2
  • Go 1.15.1
  • Swagger 2.0

不带参数跟Header请求

示例接口:http://localhost/api/get
Go-Router:v2.GET("/api/get", api.GetApi)

// @Summary 接口描述
// @Tags 分类名称
// @Accept application/json
// @Success 200 object result.Response 返回值
// @Router /api/get [get]
func GetApi(c *gin.Context) {
    res := service.GetApi()
    c.JSON(200, res)
}

带Url参数请求

示例接口:http://localhost/api/get?userId=123
Go-Router:v2.GET("/api/get", api.GetApi)

// @Summary 接口描述
// @Tags 分类名称
// @Accept application/json
// @Param userId query integer true "用户ID"
// @Success 200 object result.Response 返回值
// @Router /api/get [get]
func GetApi(c *gin.Context) {
    userId:=c.Query("userId") //查询请求URL后面的参数
    res := service.GetApi(userId)
    c.JSON(200, res)
}

带Path参数请求

示例接口:http://localhost/api/get/123
Go-Router:v2.GET("/api/get/:userId", api.GetApi)

// @Summary 接口描述
// @Tags 分类名称
// @Accept application/json
// @Param userId path integer true "用户ID"
// @Success 200 object result.Response 返回值
// @Router /api/get/{userId} [get]
func GetApi(c *gin.Context) {
    userId:=c.Param("userId") //查询路径Path参数
    res := service.GetApi(userId)
    c.JSON(200, res)
}

带Token请求

示例接口:http://localhost/api/get/123
Go-Router:v2.GET("/api/get/:userId", api.GetApi)

// @Summary 接口描述
// @Tags 分类名称
// @Accept application/json
// @Param token header string true "登录信息"
// @Success 200 object result.Response 返回值
// @Router /api/get [get]
func GetApi(c *gin.Context) {
    userId,_ :=c.Get("tokenUserId") //从解析token获取用户Id
    res := service.GetApi(userId)
    c.JSON(200, res)
}

带Body参数跟Token请求

示例接口:http://localhost/api/post
Go-Router:v2.POST("/api/post", api.PostApi)

// @Summary 接口描述
// @Tags 分类名称
// @Accept application/json
// @Param token header string true "登录信息"
// @Param data body service.ReqData true "请求参数结构体"
// @Success 200 object result.Response 返回值
// @Router /api/post [post]
func PostApi(c *gin.Context) {
    var req service.ReqData
    err := c.BindJSON(&req)
    if err!=nil{
        c.JSON(200, result.ReturnFailMsg("获取参数失败"))
    }else {
        userId,_ :=c.Get("tokenUserId") //从解析token获取用户Id
        req.UserId = userId
        res := req.PostApi()
        c.JSON(200,res)
    }
}

带文件跟Token请求

示例接口:http://localhost/api/file
Go-Router:v2.POST("/api/file", api.PostFile)

// @Summary 接口描述
// @Tags 分类名称
// @Accept application/json
// @Param token header string true "登录信息"
// @Param data formData file true "文件"
// @Success 200 object result.Response 返回值
// @Router /api/file [post]
func PostFile(c *gin.Context) {
    file, _ := c.FormFile("file")
    res := req.PostFile(file)
    c.JSON(200,res)
}

参考文档:https://github.com/swaggo/swag
原文:http://www.idmiss.com/669

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇