1

swagger.json 仕様がどのように機能するかを理解するのに忙しい (私の場合は api.json)。調査中に、GET リクエストを処理する方法の例を多数見つけることができましたが、POST などについては何も見つかりませんでした。私の当面の必要性は POST 部分を実装することですが、コードをコピーして貼り付け、試行錯誤に頼って機能させるのではなく、これをよりよく理解する必要があると感じています. Swagger.io Web サイトのコンテンツは初心者向けではありません。以下のコード例で何が起こっているのか、特に「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 の例も教えてください。

4

1 に答える 1

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"
        }
      }

デフォルトは、他に何も返されない場合のデフォルトの応答です。

    }
  }
于 2015-06-04T23:27:28.940 に答える