Go Swagger安装及使用

地址:
https://github.com/swaggo/gin-swagger
安装
- 根据go 版本使用命令
- 1.70之前
go get -u github.com/swaggo/swag/cmd/swag
- 1.70之后
go install github.com/swaggo/swag/cmd/swag@latest
查看是否成功
swag -v  
swag version v1.8.12
其他安装命令
go get -u github.com/swaggo/gin-swagger   
go get -u github.com/swaggo/files
# 模版
go get -u github.com/alecthomas/template
注解说明
添加注解
// List
// @Summary 获取多个标签
// @Produce json
// @Param name query string false "标签名称" maxlength(100)
// @Param state query int false "状态" Enums(0, 1) default(1)
// @Param page query int false "页码"
// @Param page_size query int false "每业数量"
// @Success 200 {object} model.Tag "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误",
// @Router /api/v1/tags [get]
func (t Tag) List(c *gin.Context)   {}
// Create
// @Summary 创建标签
// @Produce json
// @Param name body string false "标签名称" minlength(3) maxlength(100)
// @Param state body int false "状态" Enums(0, 1) default(1)
// @Param creator body string true "创建着" minlength(3) maxlength(100)
// @Success 200 {object} model.Tag "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags [post]
func (t Tag) Create(c *gin.Context) {}
// Update
// @Summary 更新标签
// @Produce json
// @Param id path int true "标签id"
// @Param name body string false "标签名称" minlength(3) maxlength(100)
// @Param state body int false "状态" Enums(0, 1) default(1)
// @Param updater body string true "修改人" minlength(3) maxlength(100)
// @Success 200 {array} model.Tag "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags/{id} [put]
func (t Tag) Update(c *gin.Context) {}
// Delete
// @Summary 删除标签
// @Produce json
// @Param id path int true "标签id"
// @Success 200 {string} string "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags/{id} [delete]
func (t Tag) Delete(c *gin.Context) {}
main.go 添加文档说明
// @title           Swagger Example API
// @version         1.0
// @description     This is a sample server celler server.
// @termsOfService  http://swagger.io/terms/
// @contact.name   API Support
// @contact.url    http://www.swagger.io/support
// @contact.email  support@swagger.io
// @license.name  Apache 2.0
// @license.url   http://www.apache.org/licenses/LICENSE-2.0.html
// @host      localhost:8080
// @BasePath  /api/v1
// @securityDefinitions.basic  BasicAuth
// @externalDocs.description  OpenAPI
// @externalDocs.url          https://swagger.io/resources/open-api/
func main() {
}
生产文档
swag int


路由设置文档访问路由
// 引入
import (
    ...
	swaggerFiles "github.com/swaggo/files"     // swagger embed files
	ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
)
// 路由方法
// 设置访问api文档
	//url:= ginSwagger.URL("http://127.0.0.1:8080/swagger/doc.json")
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

访问API文档: http://127.0.0.1:8080/swagger/index.html


需要将docs目录引入,即可解决问题
import (
	_ "blog-service/docs"
)

成功显示

 
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号