其他分享
首页 > 其他分享> > golang gin框架 swag在线api文档

golang gin框架 swag在线api文档

作者:互联网

一 安装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

 

 

 

 

标签:swag,golang,api,go,gin,testswag,main,com
来源: https://www.cnblogs.com/bushuwei/p/15137619.html