【问题标题】:jsend protocol Swagger defiitionjsend 协议 Swagger 定义
【发布时间】:2020-04-04 20:26:16
【问题描述】:

Jsend 协议是如何在 REST API 中格式化 json 响应的简单“标准”。 https://github.com/omniti-labs/jsend

我正在使用 https://github.com/swaggo/swag 生成 Swagger 文档,但在弄清楚如何使用声明性评论格式来描述 Jsend 响应时遇到了很大的麻烦。

如果有人这样做,我将非常感谢他们如何使用 swag 声明性注释格式定义 jsend 响应的示例 sn-p。

【问题讨论】:

    标签: swagger jsend


    【解决方案1】:

    我通过切换到更易于处理语法的goswagger.io 解决了这个问题。这些是用于基本 jsend 类型的模型。对于其他响应,我将 Data 元素替换为相关结构的名称,其余部分由 swagger 完成。

    // Success: no response data required
    // swagger:response responseSuccess
    type responseSuccess struct {
        // in: body
        Body struct {
            // enum: success
            Status string      `json:"status"`
            Data   interface{} `json:"data"`
        } `json:"body"`
    }
    
    // Error: Incorrect use of the API or the requested data is not available
    // swagger:response responseError
    type responseError struct {
        // in: body
        Body struct {
            // enum: error
            Status  string      `json:"status"`
            Data    interface{} `json:"data"`
            Message string      `json:"message"`
        } `json:"body"`
    }
    
    // Fail: Backend or system failure.
    // swagger:response responseFail
    type responseFail struct {
        // in: body
        Body struct {
            // enum: fail
            Status  string      `json:"status"`
            Data    interface{} `json:"data"`
            Message string      `json:"message"`
        } `json:"body"`
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-07
      • 2021-03-09
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多