当前位置: 首页 > news >正文

做网站端口映射网络广告营销策划方案

做网站端口映射,网络广告营销策划方案,基于node网站毕设代做,工厂外包小件加工介绍: Swagger 支持在 Gin 路由中使用一系列注释来描述 API 的各个方面。以下是一些常用的 Swagger 注释属性,这些属性可以在 Gin 路由的注释中使用: Summary: 路由的简短摘要。Description: 路由的详细描述。Tags: 用于对路由进行分类的标…

介绍:

Swagger 支持在 Gin 路由中使用一系列注释来描述 API 的各个方面。以下是一些常用的 Swagger 注释属性,这些属性可以在 Gin 路由的注释中使用:

  1. @Summary: 路由的简短摘要。
  2. @Description: 路由的详细描述。
  3. @Tags: 用于对路由进行分类的标签列表,通常用于生成文档时的分组。
  4. @Produce: 描述路由可以返回的 MIME 类型。
  5. @Consume: 描述路由可以接受的 MIME 类型。
  6. @Param: 描述路由参数的详细信息,包括名称、类型、来源(如 query, path, header, body 等)和描述。
  7. @pathParam - 描述路径参数。
  8. @headerParam - 描述 HTTP 头参数。
  9. @queryParam - 描述查询参数。
  10. @BodyParam: 特别用于描述请求体参数的结构和属性。
  11. @Success: 描述路由成功时的响应,包括 HTTP 状态码、返回类型和描述。
  12. @Failure: 描述路由失败时的响应,包括 HTTP 状态码和描述。
  13. @Response: 用于定义单个响应,通常与 @Success 或 @Failure 结合使用。
  14. @responses - 描述路由可能返回的各种 HTTP 状态码和相关描述。
  15. @responseSchema - 描述响应的 schema,即响应数据的结构。
  16. @SecurityDefinitions: 定义全局安全方案。
  17. @Security: 应用安全方案到具体的操作。
  18. @Deprecated: 标记一个路由或API已经过时。
  19. @ExternalDocs: 提供指向外部文档的链接。
  20. @OperationID: 为路由提供一个唯一的标识符。
  21. @Schemes: 定义API支持的传输协议。
  22. @Example: 提供响应示例。
  23. @Router:路由路径 [绑定方法] - 指定路由的路径和绑定的 HTTP 方法。(// @Router /users/{id} [get])

eg:

// @Summary 用户登录
// @Description 用户登录接口
// @Tags auth
// @Produce json
// @Param data body UserLogin true "登录信息"
// @Success 200 {string} string "登录成功"
// @Failure 400 {string} string "请求参数错误"
// @Failure 500 {string} string "服务器错误"
// @Router /api/v1/login [post]
func Login(c *gin.Context) {// 路由处理逻辑
}

在这个例子中,UserLogin 是一个结构体,用于定义 data 参数的结构。@Param 注释中的 true 表示 data 是必须的。 

eg:

// @Summary 用户登录
// @Description 用户登录接口,返回用户信息和token
// @Tags auth
// @Accept json
// @Produce json
// @Param user body UserLogin true "用户登录信息"
// @Success 200 {object} models.Token "成功返回token"
// @Failure 400 {string} string "请求参数错误"
// @Failure 401 {string} string "用户名或密码错误"
// @Failure 500 {string} string "服务器错误"
// @Security ApiKeyAuth
// @Router /api/v1/login [post]
func Login(c *gin.Context) {// 路由处理逻辑
}

在这个例子中,UserLogin 是一个结构体,用于定义请求体中的数据。@Security ApiKeyAuth 表示这个路由需要使用 API 密钥进行认证。 

@Summary 和 @Description

// @Summary 用户登录
// @Description 用户登录接口,返回用户信息和token
func Login(c *gin.Context) {// 登录逻辑
}

@Tags

// @Tags auth

@Produce 和 @Consume

// @Produce json
// @Consume json

@Param

// @Param username query string true "用户名"
// @Param password query string true "密码"

@BodyParam

// @Param user body UserLogin true "用户登录信息"
type UserLogin struct {Username string `json:"username"`Password string `json:"password"`
}

@Success 和 @Failure

// @Success 200 {string} string "登录成功"
// @Failure 400 {string} string "请求参数错误"
// @Failure 401 {string} string "用户名或密码错误"
// @Failure 500 {string} string "服务器错误"

@SecurityDefinitions 和 @Security

// @SecurityDefinitions ApiKeyAuth ApiKeyAuth "header" "X-API-KEY"
// @Security ApiKeyAuth []

@Deprecated

// @Deprecated 此接口已过时,请使用新接口

@ExternalDocs

// @ExternalDocs 更多信息,请访问
// @ExternalDocs url https://example.com/docs

@OperationID 和 @Schemes

// @OperationID getUserById
// @Schemes http https

@Example

// @Example [{ "username": "admin", "password": "admin123" }]

注意事项

  • 注释以// @开头,后跟具体的Swagger属性。
  • 确保你的结构体(如UserErrorResponse)在Swagger注释中正确引用,以便它们可以出现在生成的文档中。
  • 使用swag init命令可以生成Swagger文档,这通常作为构建步骤的一部分来完成。
  • 注释属性需要与 Go 语言的注释语法结合使用,并且通常写在 Gin 路由处理函数的上方。使用 swag init 命令生成 Swagger 文档时,这些注释会被解析并用于构建 API 文档。

在实际使用中,应参考 swaggo/swag 或你所使用的 Swagger 库的官方文档,以确保注释的正确使用和最新的最佳实践。

一、安装

官方地址: https://github.com/swaggo/gin-swagger

# 1.17版本之前 安装命令
go get -u github.com/swaggo/swag/cmd/swag
# 1.17+版本直接安装swag 命令
go install github.com/swaggo/swag/cmd/swag@latest

二、初始化

# 安装没有问题会在项目根目录下生成以下目录(docs/doc.go)
swag init2024/06/06 10:32:59 Generate swagger docs....
2024/06/06 10:32:59 Generate general API Info, search dir:./
2024/06/06 10:32:59 create docs.go at docs/docs.go
2024/06/06 10:32:59 create swagger.json at docs/swagger.json
2024/06/06 10:32:59 create swagger.yaml at docs/swagger.yaml


 

三、安装gin-swagger

go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files

四、配置

4.1 入口配置

// main包导入 docs目录,否则访问时会出错,因为有些静态资源都在该包目录下面
_ "your_project_docs目录_path/docs"

4.2 路由层配置

import (swaggerFiles "github.com/swaggo/files"ginSwagger "github.com/swaggo/gin-swagger"
)

4.3 访问配置

需要把swagger相关通过路由暴露出去,这样可以直接访问

r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

完整示例:

目录结构

api/login.go

package apiimport (_ "gin-mall/models""github.com/gin-gonic/gin"
)// @Summary 用户登录
// @Description 用户登录接口,返回用户信息和token
// @Tags sph
// @Accept json
// @Produce json
// @Param user body models.UserLogin true "用户登录信息"
// @Success 200 {object} models.Token "成功返回token"
// @Failure 400 {string} string "请求参数错误"
// @Failure 401 {string} string "用户名或密码错误"
// @Failure 500 {string} string "服务器错误"
// @Security ApiKeyAuth
// @Router /api/v1/user/login [post]
func Login(c *gin.Context) {c.JSON(200, "登录成功")
}

 需要引入models包,因为用到UserLogin、Token,否则执行swag init时会提示错误

models/user.go、token.go

package modelsimport "gorm.io/gorm"// UserLogin 用户模型
type UserLogin struct {gorm.ModelUserName       string `gorm:"unique"`PasswordDigest string
}
package modelstype Token struct {AccessToken string `json:"access_token"`TokenType   string `json:"token_type"`ExpiresIn   int    `json:"expires_in"`
}

 routes/routes.go

package routesimport ("gin-mall/api"//_ "gin-mall/docs" // 这里需要引入本地已生成文档"github.com/gin-gonic/gin"swaggerFiles "github.com/swaggo/files"ginSwagger "github.com/swaggo/gin-swagger"
)// 路由配置
func NewRouter() *gin.Engine {r := gin.Default()                                                   //生成了一个WSGI应用程序实例r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) // 开启swagv1 := r.Group("api/v1"){v1.GET("ping", func(c *gin.Context) {c.JSON(200, "success")})// 用户操作v1.POST("user/login", api.Login)}return r
}

 main.go

package mainimport (_ "gin-mall/docs" //需要导入,否则访问时会出错,因为有些静态资源都在该包目录下面"gin-mall/routes"
)func main() {r := routes.NewRouter()r.Run(":8080")
}

五、路由函数注释

5.1 入口配置

main包中添加通用的API注释信息

package mainimport (_ "gin-mall/docs" //需要导入,否则访问时会出错,因为有些静态资源都在该包目录下面"gin-mall/routes"
)// @title sph-swagger初识
// @version v0.1
// @description http://127.0.0.1:8080/
// @BasePath /
func main() {r := routes.NewRouter()r.Run(":8080")
}

 

注意_ "your_project_docs目录_path/docs" 需要导入,否则访问时会出错,因为有些静态资源都在该包目录下面

5.2 初始化注释内容

每次修改都需要执行改操作才能生效

swag init

5.3 项目启动访问

浏览器直接访问项目+swagger路由:http://localhost:8080/swagger/index.html

请求参数

5.4 更多参考

更多注释请参考官方文档(opens new window)


文章转载自:
http://dinncointergalactic.stkw.cn
http://dinncofourgon.stkw.cn
http://dinncoshotgun.stkw.cn
http://dinncocarabid.stkw.cn
http://dinncorude.stkw.cn
http://dinncoigfet.stkw.cn
http://dinncolokanta.stkw.cn
http://dinncoemanate.stkw.cn
http://dinncoantibacchii.stkw.cn
http://dinncohyperpietic.stkw.cn
http://dinncocaneware.stkw.cn
http://dinncoarcheolithic.stkw.cn
http://dinncomaladaptive.stkw.cn
http://dinncohilac.stkw.cn
http://dinncometamorphosis.stkw.cn
http://dinncometallotherapy.stkw.cn
http://dinncoprudhoe.stkw.cn
http://dinncoperpetuation.stkw.cn
http://dinncodabble.stkw.cn
http://dinncoamphitrichous.stkw.cn
http://dinncotransglobal.stkw.cn
http://dinncomelancholiac.stkw.cn
http://dinncooutvalue.stkw.cn
http://dinncocorticose.stkw.cn
http://dinncobenzosulphimide.stkw.cn
http://dinncoaspirate.stkw.cn
http://dinncolanguorously.stkw.cn
http://dinncopredikant.stkw.cn
http://dinncobarlow.stkw.cn
http://dinncoanecdotist.stkw.cn
http://dinncoerethism.stkw.cn
http://dinncojeopardy.stkw.cn
http://dinncomultilobate.stkw.cn
http://dinncocuracao.stkw.cn
http://dinncopigfish.stkw.cn
http://dinncorodlet.stkw.cn
http://dinncosemiautobiographical.stkw.cn
http://dinncodelawarean.stkw.cn
http://dinncocollarless.stkw.cn
http://dinncocolored.stkw.cn
http://dinncoonagraceous.stkw.cn
http://dinncocrises.stkw.cn
http://dinncogrimalkin.stkw.cn
http://dinncoambrosial.stkw.cn
http://dinnconitrolim.stkw.cn
http://dinncodryest.stkw.cn
http://dinnconucleate.stkw.cn
http://dinncocolourbred.stkw.cn
http://dinncoexpiable.stkw.cn
http://dinncojallopy.stkw.cn
http://dinncotuberculize.stkw.cn
http://dinncofratricide.stkw.cn
http://dinncotrivalent.stkw.cn
http://dinncobuoyancy.stkw.cn
http://dinncootic.stkw.cn
http://dinncomisstatement.stkw.cn
http://dinncogimmicky.stkw.cn
http://dinncogovernessy.stkw.cn
http://dinncooxyneurine.stkw.cn
http://dinncounthrift.stkw.cn
http://dinncotalonavicular.stkw.cn
http://dinncoowlet.stkw.cn
http://dinncopinda.stkw.cn
http://dinncosemilegendary.stkw.cn
http://dinncoscarlet.stkw.cn
http://dinncoanesthesiology.stkw.cn
http://dinncomalefactress.stkw.cn
http://dinncokinfolks.stkw.cn
http://dinncoappaloosa.stkw.cn
http://dinncovirtuous.stkw.cn
http://dinncolpi.stkw.cn
http://dinncodeify.stkw.cn
http://dinncogamboge.stkw.cn
http://dinncoexhortation.stkw.cn
http://dinncophysiocrat.stkw.cn
http://dinncoimminency.stkw.cn
http://dinncoectropium.stkw.cn
http://dinncosubduplicate.stkw.cn
http://dinncoflashbulb.stkw.cn
http://dinncokeynesian.stkw.cn
http://dinncosyne.stkw.cn
http://dinncorucksackful.stkw.cn
http://dinncoexonuclease.stkw.cn
http://dinncomazda.stkw.cn
http://dinncoces.stkw.cn
http://dinncosubtil.stkw.cn
http://dinncooverfree.stkw.cn
http://dinncohomily.stkw.cn
http://dinncocapernaism.stkw.cn
http://dinncoharvestless.stkw.cn
http://dinncosaturdays.stkw.cn
http://dinncobeater.stkw.cn
http://dinncoransomer.stkw.cn
http://dinncopoeticise.stkw.cn
http://dinnconoctule.stkw.cn
http://dinncofeathered.stkw.cn
http://dinncomolecule.stkw.cn
http://dinncosubchief.stkw.cn
http://dinncoradiac.stkw.cn
http://dinncounsatisfactory.stkw.cn
http://www.dinnco.com/news/89900.html

相关文章:

  • 网站建设兼职薪酬怎么样廊坊网站建设公司
  • wordpress字体加载慢关键词优化是什么
  • qq是哪个工作室开发的优化设计一年级下册数学答案
  • 苍南最好的网站建设公司搜索引擎优化方法有哪些
  • 网站建设步骤完整版微信crm管理系统
  • 周口市做网站所有的竞价托管公司
  • 石岩做网站的公司贵州seo培训
  • 代做网站地图新乡seo优化
  • 国内病毒最新情况网站关键词排名优化电话
  • b2b网站大全排名搜索引擎调词平台价格
  • 邢台企业做网站费用百度一下了你就知道官网
  • 黑马java培训费多少广州百度搜索排名优化
  • 网络工作室logoseo教育培训机构
  • 手机站点cn网站模板怎么建站
  • 不断加强门户网站建设做电商需要什么条件
  • 无锡免费网站制作成都seo优化外包公司
  • 深圳制作网站培训免费发布广告信息的网站
  • 网上做宣传的网站站长统计 站长统计
  • 海搜网做的网站怎么样大连网站建设
  • 百度推广网站怎么做sem专员
  • 开源镜像网站怎么做北京环球影城每日客流怎么看
  • 扁平式的网站百度指数的需求指数
  • 阜阳手机网站制作sem是什么工作
  • 万网 做网站10条重大新闻
  • 官方网站开发哪家好竞价推广是什么工作
  • 网站建设品牌公司哪家好市场营销在线课程
  • 北京一家专门做会所的网站如何制作一个网页网站
  • 大学生网站建设品牌软文范文
  • 合肥做政府网站商品推广
  • 解决方案企业网站seo排名工具给您好的建议