Swagger UI 仍在展示示例
Swagger UI still showing the example
我想将 Swagger 用于我的 RESTFul API 来自 Go 和 Gin 的文档。
我在 main.go 中有这段代码:
主包
import (
"gowebservice/config"
"gowebservice/controllers"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
_ "github.com/swaggo/gin-swagger/example/basic/docs"
)
// @title Students API
// @version 1.0
// @description This is a basic API Students using Gin and Gorm.
// @host localhost:8080
// @BasePath /
func main() {
r := gin.Default()
config.ConnectDatabase()
v1 := r.Group("/api/v1")
{
v1.GET("students/all", controllers.GetStudents)
}
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.Run()
}
这是我的具有 GET 方法的端点:
package controllers
import (
"gowebservice/config"
"gowebservice/models"
"net/http"
"github.com/gin-gonic/gin"
)
// GetStudents godoc
// @Summary Show a list of students
// @Accept json
// @Produce json
// @Router /students [get]
func GetStudents(c *gin.Context) {
var students []models.Student
if err := config.DB.Find(&students).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
c.JSON(http.StatusOK, students)
}
当我使用 swag init
和 go run main.go
时,Swagger UI 仍然显示示例而不是我的端点。
谁能帮帮我?
是否生成了这些文件?
docs.go swagger.json swagger.yaml
swag help init
NAME:
swag init - Create docs.go
USAGE:
swag init [command options] [arguments...]
OPTIONS:
--generalInfo value, -g value Go file path in which 'swagger general API Info' is written (default: "main.go")
--dir value, -d value Directory you want to parse (default: "./")
--propertyStrategy value, -p value Property Naming Strategy like snakecase,camelcase,pascalcase (default: "camelcase")
--output value, -o value Output directory for all the generated files(swagger.json, swagger.yaml and doc.go) (default: "./docs")
--parseVendor Parse go files in 'vendor' folder, disabled by default
--parseDependency Parse go files in outside dependency folder, disabled by default
--markdownFiles value, --md value Parse folder containing markdown files to use as description, disabled by default
--generatedTime Generate timestamp at the top of docs.go, true by default
我运行也喜欢这个
问题是我们导入了基本示例文档:
import (
...
_ "github.com/swaggo/gin-swagger/example/basic/docs"
)
您需要将其更改为 swag init
生成的文档包。
假设你的模块名称是gowebservice
,那么:
import (
...
_ "gowebservice/docs"
)
运行 go run main.go
之后应该让 Swagger UI 找到您自己的文档! :D
我想将 Swagger 用于我的 RESTFul API 来自 Go 和 Gin 的文档。
我在 main.go 中有这段代码: 主包
import (
"gowebservice/config"
"gowebservice/controllers"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
_ "github.com/swaggo/gin-swagger/example/basic/docs"
)
// @title Students API
// @version 1.0
// @description This is a basic API Students using Gin and Gorm.
// @host localhost:8080
// @BasePath /
func main() {
r := gin.Default()
config.ConnectDatabase()
v1 := r.Group("/api/v1")
{
v1.GET("students/all", controllers.GetStudents)
}
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.Run()
}
这是我的具有 GET 方法的端点:
package controllers
import (
"gowebservice/config"
"gowebservice/models"
"net/http"
"github.com/gin-gonic/gin"
)
// GetStudents godoc
// @Summary Show a list of students
// @Accept json
// @Produce json
// @Router /students [get]
func GetStudents(c *gin.Context) {
var students []models.Student
if err := config.DB.Find(&students).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
c.JSON(http.StatusOK, students)
}
当我使用 swag init
和 go run main.go
时,Swagger UI 仍然显示示例而不是我的端点。
谁能帮帮我?
是否生成了这些文件?
docs.go swagger.json swagger.yaml
swag help init
NAME:
swag init - Create docs.go
USAGE:
swag init [command options] [arguments...]
OPTIONS:
--generalInfo value, -g value Go file path in which 'swagger general API Info' is written (default: "main.go")
--dir value, -d value Directory you want to parse (default: "./")
--propertyStrategy value, -p value Property Naming Strategy like snakecase,camelcase,pascalcase (default: "camelcase")
--output value, -o value Output directory for all the generated files(swagger.json, swagger.yaml and doc.go) (default: "./docs")
--parseVendor Parse go files in 'vendor' folder, disabled by default
--parseDependency Parse go files in outside dependency folder, disabled by default
--markdownFiles value, --md value Parse folder containing markdown files to use as description, disabled by default
--generatedTime Generate timestamp at the top of docs.go, true by default
我运行也喜欢这个
问题是我们导入了基本示例文档:
import (
...
_ "github.com/swaggo/gin-swagger/example/basic/docs"
)
您需要将其更改为 swag init
生成的文档包。
假设你的模块名称是gowebservice
,那么:
import (
...
_ "gowebservice/docs"
)
运行 go run main.go
之后应该让 Swagger UI 找到您自己的文档! :D