除 HTTP GET 之外的任何 Swagger 规范

问题描述 投票:0回答:2

我正忙于了解 swagger.json 规范的工作原理(在我的例子中是 api.json)。在研究过程中,我可以找到许多有关如何处理 GET 请求的示例,但没有找到有关 POST 或其他任何内容的示例。我迫切需要的是实现 POST 部分,但我觉得我需要更好地理解这一点,而不是复制和粘贴代码并依靠反复试验来使其工作。 Swagger.io 网站中的内容对初学者不友好。有人可以解释下面的示例代码中发生了什么,特别是在两种情况下的“get:”之后:

{
"swagger": "2.0",
"info": {
    "version": "v1",
    "title": "FoodTrucks",
"description": "An TryApp sample API App where you can find Food trucks."
},
"host": "microsoft-apiapp1fe6951749ff4b76b8cc9194bc29ba61.azurewebsites.net:443",
"schemes": ["http", "https"],
"basePath": "/api/data",
"paths": {
    "/foodtrucks": {
        "get": {
            "tags": ["FoodTrucks"],
            "operationId": "getFoodTrucks",
            "consumes": [],
            "produces": ["application/json",
            "text/json",
            "application/xml",
            "text/xml"],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "type": "array",
                        "items": {
                            "$ref": "#/definitions/Restaurant"
                        }
                    }
                }
            },
            "deprecated": false
        }
    },
    "/foodtrucks/{id}": {
        "get": {
            "tags": ["FoodTrucks"],
            "operationId": "getFoodTruckDetails",
            "consumes": [],
            "produces": ["application/json",
            "text/json",
            "application/xml",
            "text/xml"],
            "parameters": [{
                "name": "id",
                "in": "path",
                "required": true,
                "type": "integer",
                "format": "int32"
            }],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "type": "array",
                        "items": {
                            "$ref": "#/definitions/Restaurant"
                        }
                    }
                }
            },
            "deprecated": false
        }
    }
},
"definitions": {
    "Restaurant": {
        "type": "object",
        "properties": {
            "id": {
                "format": "int32",
                "type": "integer"
            },
            "name": {
                "type": "string"
            },
            "likes": {
                "format": "int32",
                "type": "integer"
            },
            "savory": {
                "type": "boolean"
            },
            "sweet": {
                "type": "boolean"
            },
            "vegetarian": {
                "type": "boolean"
            },
            "bookable": {
                "type": "boolean"
            },
            "city": {
                "type": "string"
            }
        }
    }
 }
}

请您也帮忙提供一个简单的 POST 示例。

rest http-post swagger swagger-editor swagger-2.0
2个回答
2
投票

Swagger 的扩展示例包括 POST。这是一个带有逐块解释的示例:

"post": {
    "description": "Creates a new pet in the store.  Duplicates are allowed",

描述是对操作的友好解释,主要取自文档

    "operationId": "addPet",

OperationId 是操作的友好名称。必须是独一无二的。

    "produces": [
      "application/json"
    ],

Produces 是端点输出的 MIME 类型

    "parameters": [
      {
        "name": "pet",
        "in": "body",
        "description": "Pet to add to the store",
        "required": true,
        "schema": {
          "$ref": "#/definitions/NewPet"

模式引用($ref)是指“定义”块中的数据类型定义。请参阅此 JSON 中的 NewPet 部分。

        }
      }
    ],

参数在文档的参数块中得到了最好的描述。

    "responses": {
      "200": {
        "description": "pet response",
        "schema": {
          "$ref": "#/definitions/Pet"
        }
      },

同样,响应最好在 响应文档中描述

      "default": {
        "description": "unexpected error",
        "schema": {
          "$ref": "#/definitions/ErrorModel"
        }
      }
如果没有其他返回,则

默认是默认响应

    }
  }

0
投票

需要帮助恢复我的帐户授权访问权限。当我还是个孩子的时候,未经我的授权,我就被置于家长模式。这就是我的真实 帐户。另外还需要另外两个帐户的帐户,这些帐户是未经我授权而回购的

© www.soinside.com 2019 - 2024. All rights reserved.