golang gin框架 swag在线api文档

时间:2021-08-13 17:16:26   收藏:0   阅读:29

一 安装swag

go get github.com/swaggo/swag/cmd/swag

 

二 安装gin-swagger

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

go get -u github.com/swaggo/gin-swagger/swaggerFiles

 

三 生成docs文件夹

 

swag init  

技术分享图片

 

四 gin引用swag

 

  main.go

package main

import (
    "debuggrpc/test/testswag/controller"
    "github.com/gin-gonic/gin"
    "github.com/swaggo/gin-swagger/swaggerFiles"

    ginSwagger "github.com/swaggo/gin-swagger"

    _ "debuggrpc/test/testswag/docs"

)

// @title TestSwg API
// @version 1.0

// @host 127.0.0.1:9501
// @BasePath /testswag/v1.0
func main(){
    router := gin.Default()
    // v1
    r1 := router.Group("/testswag/v1.0")
    {
        r1.GET("/Baseinfo", controller.Baseinfo)

    }
    router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
    router.Run( "0.0.0.0:8080")
}

  base.go

 

package controller

import (
    "github.com/gin-gonic/gin"
    "net/http"
)

// @Summary 获取基础信息列表
// @Description 获取基础信息列表
// @Tags 基础信息
// @Accept json
// @Success 200 {json} json "{"errno":0,"errmsg": "成功","data":infolist}"
// @Failure 400 {json} json "{"errno":4502,"errmsg": "查询失败"}"
// @Router /Baseinfo [post]
func Baseinfo(ctx *gin.Context) {
    resp := make(map[string]interface{})
    resp["data"] = "baseinfo"
    resp["errno"] = 200
    resp["errmsg"] = "success"
    ctx.JSON(http.StatusOK, resp)
    return
}

  运行main.go之后,浏览器分别访问 http://localhost:8080/testswag/v1.0/Baseinfo   

技术分享图片

 

http://localhost:8080/swagger/index.html

技术分享图片

 

 

 

原文:https://www.cnblogs.com/bushuwei/p/15137619.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!