全面指南:轻松安装与使用 Gin Swag,解答常见问题
最编程
2024-08-07 15:42:42
...
gin-swagger 安装&使用手册&问题
官方地址
https://github.com/swaggo/gin-swagger
安装教程
#!/bin/bash
# install swag
go get -u github.com/swaggo/swag/cmd/swag
#Download gin-swagger by using:
go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files
- init
swag init
特别提醒
swag 之后会默认生成docs目录,请在项目中导入
import _ "Your/Project/RootPath/docs"
API信息
官方文档: https://swaggo.github.io/swaggo.io/declarative_comments_format/
快速上手
gin-swagger
import (
_ "Your/Project/RootPath/docs"
"github.com/gin-gonic/gin"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
)
// @title ioc api
// @version v0.0.1
// @description null
// @termsOfService http://swagger.io/terms/
// @contact.name puhao
// @contact.url puhao
// @contact.email puhao
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host 0.0.0.0:8989
// @BasePath
func main() {
r := gin.Default()
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.Run("0.0.0.0:8989")
}
API Info
// Login godoc
// @Summary login with user and password, get token
// @Tags login
// @version 1.0
// @Accept application/x-json-stream
// @Param user query string true "user name"
// @Param pd query string true "user's password"
// @Router /openapi/login [get]
// @Success 200 {object} YOURObject "{"code":200,"data": {"token": ""},"msg":""}"
// @Failure 403 {object} YOURObject {"code":200,"data":null,"msg":""}
func Login(c *gin.Context) {
//...Your logic
}
遇到的问题
Type definition of type ‘*ast.InterfaceType’ is not supported yet. Using ‘object’ instead.
go get -u github.com/swaggo/swag/cmd/swag@v1.6.7
Failed to load spec. (同时会有panic信息)
import _ "Your/Project/RootPath/docs"
TypeError: Failed to fetch
摁F12,看报错详情,一般会有 has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’,那么这是跨域问题
推荐阅读
-
全面指南:轻松安装与使用 Gin Swag,解答常见问题
-
【2022新手指南】Java编程进阶之路 - 六、技术架构篇 ### MySQL索引底层解析与优化实战 - 你会讲解MySQL索引的数据结构吗?性能调优技巧知多少? - Redis深度揭秘:你知道多少?从基础到哨兵、主从复制全梳理 - Redis持久化及哨兵模式详解,还有集群搭建和Leader选举黑箱打开 - Zookeeper是个啥?特性和应用场景大公开 - ZooKeeper集群搭建攻略及 Leader选举、读写一致性、共享锁实现细节 - 探究ZooKeeper中的Leader选举机制及其在分布式环境中的作用 - Zab协议深入剖析:原理、功能与在Zookeeper中的核心地位 - RabbitMQ全方位解读:工作模式、消费限流、可靠投递与配置策略 - 设计者视角:RabbitMQ过期时间、死信队列与延时队列实践指南 - RocketMQ特性和应用场景揭示:理解其精髓与差异化优势 - Kafka详细介绍:特性及广泛应用于实时数据处理的场景解析 - ElasticSearch实力揭秘:特性概述与作为搜索引擎的广泛应用 - MongoDB认知升级:非关系型数据库的优势阐述,安装与使用实战教学 - BIO/NIO/AIO网络模型对比:掌握它们的区别与在网络编程中的实际应用 - Netty带你飞:理解其超快速度背后的秘密,包括线程模型分析 - 网络通信黑科技:Netty编解码原理与常用编解码器的应用,Protostuff实战演示 - 解密Netty粘包与拆包现象,怎样有效应对这一常见问题 - 自定义Netty心跳检测机制,轻松调整检测间隔时间的艺术 - Dubbo轻骑兵介绍:核心特性概览,服务降级实战与其实现益处 - Dubbo三大神器解读:本地存根与本地伪装的实战运用与优势呈现 ----------------------- 七、结语与回顾
-
全面指南:轻松掌握 Ansys 2020 的安装与使用