输过败过不曾怕过 發表於 2023-6-6 23:14:00

Go Swagger安装及使用

<p><img src="https://img2023.cnblogs.com/blog/1118095/202306/1118095-20230606231817333-413213876.png" alt="" loading="lazy"></p>
<p>地址:</p>
<p><code>https://github.com/swaggo/gin-swagger</code></p>
<h3 id="安装">安装</h3>
<ol>
<li>根据go 版本使用命令</li>
</ol>
<ul>
<li>1.70之前</li>
</ul>
<pre><code class="language-sh">go get -u github.com/swaggo/swag/cmd/swag
</code></pre>
<ul>
<li>1.70之后</li>
</ul>
<pre><code class="language-sh">go install github.com/swaggo/swag/cmd/swag@latest
</code></pre>
<p>查看是否成功<br>
<code>swag -v</code><br>
swag version v1.8.12</p>
<p>其他安装命令</p>
<pre><code class="language-sh">go get -u github.com/swaggo/gin-swagger   
go get -u github.com/swaggo/files
# 模版
go get -u github.com/alecthomas/template
</code></pre>
<h3 id="注解说明">注解说明</h3>
<h3 id="添加注解">添加注解</h3>
<pre><code class="language-sh">
// 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
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
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}
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}
func (t Tag) Delete(c *gin.Context) {}

</code></pre>
<p>main.go 添加文档说明</p>
<pre><code class="language-sh">// @title         Swagger Example API
// @version         1.0
// @description   This is a sample server celler server.
// @termsOfServicehttp://swagger.io/terms/

// @contact.name   API Support
// @contact.url    http://www.swagger.io/support
// @contact.emailsupport@swagger.io

// @license.nameApache 2.0
// @license.url   http://www.apache.org/licenses/LICENSE-2.0.html

// @host      localhost:8080
// @BasePath/api/v1

// @securityDefinitions.basicBasicAuth

// @externalDocs.descriptionOpenAPI
// @externalDocs.url          https://swagger.io/resources/open-api/
func main() {
}

</code></pre>
<h3 id="生产文档">生产文档</h3>
<pre><code class="language-sh">swag int
</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/1118095/202306/1118095-20230606005509106-1717734596.png" alt="" loading="lazy"></p>
<p><img src="https://img2023.cnblogs.com/blog/1118095/202306/1118095-20230606005822287-1572059015.png" alt="" loading="lazy"></p>
<p>路由设置文档访问路由</p>
<pre><code>// 引入
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))


</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/1118095/202306/1118095-20230606231146955-1743807946.png" alt="" loading="lazy"></p>
<p>访问API文档: http://127.0.0.1:8080/swagger/index.html</p>
<p><img src="https://img2023.cnblogs.com/blog/1118095/202306/1118095-20230606230706740-1434598101.png" alt="" loading="lazy"></p>
<p><img src="https://img2023.cnblogs.com/blog/1118095/202306/1118095-20230606230632242-185086657.png" alt="" loading="lazy"></p>
<p>需要将docs目录引入,即可解决问题</p>
<pre><code>import (
        _ "blog-service/docs"
)
</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/1118095/202306/1118095-20230606230943355-2024980256.png" alt="" loading="lazy"></p>
<p>成功显示</p>
<p><img src="https://img2023.cnblogs.com/blog/1118095/202306/1118095-20230606230908670-1128611197.png" alt="" loading="lazy"></p><br><br>
来源:https://www.cnblogs.com/smallyi/p/17462043.html
頁: [1]
查看完整版本: Go Swagger安装及使用