API閘道的範例資料模型和對應範本 - Amazon API Gateway

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

API閘道的範例資料模型和對應範本

以下各節提供了模型和映射模板的示例,這些模型和映射模板可以APIs在 API Gateway 中用作您自己的起點。如需資料轉型的詳細資訊,請參閱 對應範本 REST APIs。如需資料模型的詳細資訊,請參閱資料模型 REST APIs

相簿範例

下列範例顯示 G API ateway API 中的相簿。我們提供了範例資料轉換、額外模型和對應範本。

範例資料轉換

下列範例顯示如何使用 Velocity 範本語言 (VTL) 對應範本來轉換相片的輸入資料。如需有關速度範本語言的詳細資訊,請參閱 Apache 速度-VTL 參考。

下列範例是將資料輸入至整合要求。

{ "photos": { "page": 1, "pages": "1234", "perpage": 100, "total": "123398", "photo": [ { "id": "12345678901", "owner": "23456789@A12", "photographer_first_name" : "Saanvi", "photographer_last_name" : "Sarkar", "secret": "abc123d456", "server": "1234", "farm": 1, "title": "Sample photo 1", "ispublic": true, "isfriend": false, "isfamily": false }, { "id": "23456789012", "owner": "34567890@B23", "photographer_first_name" : "Richard", "photographer_last_name" : "Roe", "secret": "bcd234e567", "server": "2345", "farm": 2, "title": "Sample photo 2", "ispublic": true, "isfriend": false, "isfamily": false } ] } }

以下示例是用於轉換照片數據的映射模板。

#set($inputRoot = $input.path('$')) { "photos": [ #foreach($elem in $inputRoot.photos.photo) { "id": "$elem.id", "photographedBy": "$elem.photographer_first_name $elem.photographer_last_name", "title": "$elem.title", "ispublic": $elem.ispublic, "isfriend": $elem.isfriend, "isfamily": $elem.isfamily }#if($foreach.hasNext),#end #end ] }

下列範例是轉換的輸出資料。

{ "photos": [ { "id": "12345678901", "photographedBy": "Saanvi Sarkar", "title": "Sample photo 1", "ispublic": true, "isfriend": false, "isfamily": false }, { "id": "23456789012", "photographedBy": "Richard Roe", "title": "Sample photo 2", "ispublic": true, "isfriend": false, "isfamily": false } ] }

相片資料的輸入模型

您可以定義輸入資料的模型。此輸入模型要求您上傳一張照片,並為每頁指定至少 10 張照片。您可以使用此輸入模型來產生SDK或開啟您的要求驗證API。

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "PhotosInputModel", "type": "object", "properties": { "photos": { "type": "object", "required" : [ "photo" ], "properties": { "page": { "type": "integer" }, "pages": { "type": "string" }, "perpage": { "type": "integer", "minimum" : 10 }, "total": { "type": "string" }, "photo": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "string" }, "owner": { "type": "string" }, "photographer_first_name" : {"type" : "string"}, "photographer_last_name" : {"type" : "string"}, "secret": { "type": "string" }, "server": { "type": "string" }, "farm": { "type": "integer" }, "title": { "type": "string" }, "ispublic": { "type": "boolean" }, "isfriend": { "type": "boolean" }, "isfamily": { "type": "boolean" } } } } } } } }

相片資料的輸出模型

您可以定義輸出資料的模型。您可以將此模型用於方法回應模型,此模型在產生的強類型時是必要SDK的API。這會導致輸出在 Java 或 Objective-C 中轉換成適當的類別。

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "PhotosOutputModel", "type": "object", "properties": { "photos": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "string" }, "photographedBy": { "type": "string" }, "title": { "type": "string" }, "ispublic": { "type": "boolean" }, "isfriend": { "type": "boolean" }, "isfamily": { "type": "boolean" } } } } } }

照片資料的輸入對應範本

您可以定義對應範本來修改輸入資料。您可以修改輸入資料,取得進一步的函數整合或整合回應。

#set($inputRoot = $input.path('$')) { "photos": { "page": $inputRoot.photos.page, "pages": "$inputRoot.photos.pages", "perpage": $inputRoot.photos.perpage, "total": "$inputRoot.photos.total", "photo": [ #foreach($elem in $inputRoot.photos.photo) { "id": "$elem.id", "owner": "$elem.owner", "photographer_first_name" : "$elem.photographer_first_name", "photographer_last_name" : "$elem.photographer_last_name", "secret": "$elem.secret", "server": "$elem.server", "farm": $elem.farm, "title": "$elem.title", "ispublic": $elem.ispublic, "isfriend": $elem.isfriend, "isfamily": $elem.isfamily }#if($foreach.hasNext),#end #end ] } }

新聞文章範例

下列範例顯示 API Gateway API 中的新聞文章。我們提供了範例資料轉換、額外模型和對應範本。

範例資料轉換

下列範例顯示如何使用 Velocity 範本語言 (VTL) 對應範本,轉換有關新聞文章的輸入資料。如需有關速度範本語言的詳細資訊,請參閱 Apache 速度-VTL 參考。

下列範例是將資料輸入至整合要求。

{ "count": 1, "items": [ { "last_updated_date": "2015-04-24", "expire_date": "2016-04-25", "author_first_name": "John", "description": "Sample Description", "creation_date": "2015-04-20", "title": "Sample Title", "allow_comment": true, "author": { "last_name": "Doe", "email": "johndoe@example.com", "first_name": "John" }, "body": "Sample Body", "publish_date": "2015-04-25", "version": "1", "author_last_name": "Doe", "parent_id": 2345678901, "article_url": "http://www.example.com/articles/3456789012" } ], "version": 1 }

下列範例是用來轉換新聞文章資料的對應範本。

#set($inputRoot = $input.path('$')) { "count": $inputRoot.count, "items": [ #foreach($elem in $inputRoot.items) { "creation_date": "$elem.creation_date", "title": "$elem.title", "author": "$elem.author.first_name $elem.author.last_name", "body": "$elem.body", "publish_date": "$elem.publish_date", "article_url": "$elem.article_url" }#if($foreach.hasNext),#end #end ], "version": $inputRoot.version }

下列範例是轉換的輸出資料。

{ "count": 1, "items": [ { "creation_date": "2015-04-20", "title": "Sample Title", "author": "John Doe", "body": "Sample Body", "publish_date": "2015-04-25", "article_url": "http://www.example.com/articles/3456789012" } ], "version": 1 }

新聞資料輸入模型

您可以定義輸入資料的模型。此輸入模型要求新聞文章包含一個URL,標題和正文。您可以使用此輸入模型來產生SDK或開啟您的要求驗證API。

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "NewsArticleInputModel", "type": "object", "properties": { "count": { "type": "integer" }, "items": { "type": "array", "items": { "type": "object", "required": [ "article_url", "title", "body" ], "properties": { "last_updated_date": { "type": "string" }, "expire_date": { "type": "string" }, "author_first_name": { "type": "string" }, "description": { "type": "string" }, "creation_date": { "type": "string" }, "title": { "type": "string" }, "allow_comment": { "type": "boolean" }, "author": { "type": "object", "properties": { "last_name": { "type": "string" }, "email": { "type": "string" }, "first_name": { "type": "string" } } }, "body": { "type": "string" }, "publish_date": { "type": "string" }, "version": { "type": "string" }, "author_last_name": { "type": "string" }, "parent_id": { "type": "integer" }, "article_url": { "type": "string" } } } }, "version": { "type": "integer" } } }

新聞數據的輸出模型

您可以定義輸出資料的模型。您可以將此模型用於方法回應模型,此模型在產生的強類型時是必要SDK的API。這會導致輸出在 Java 或 Objective-C 中轉換成適當的類別。

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "PhotosOutputModel", "type": "object", "properties": { "photos": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "string" }, "photographedBy": { "type": "string" }, "title": { "type": "string" }, "ispublic": { "type": "boolean" }, "isfriend": { "type": "boolean" }, "isfamily": { "type": "boolean" } } } } } }

新聞數據的輸入映射模板

您可以定義對應範本來修改輸入資料。您可以修改輸入資料,取得進一步的函數整合或整合回應。

#set($inputRoot = $input.path('$')) { "count": $inputRoot.count, "items": [ #foreach($elem in $inputRoot.items) { "last_updated_date": "$elem.last_updated_date", "expire_date": "$elem.expire_date", "author_first_name": "$elem.author_first_name", "description": "$elem.description", "creation_date": "$elem.creation_date", "title": "$elem.title", "allow_comment": "$elem.allow_comment", "author": { "last_name": "$elem.author.last_name", "email": "$elem.author.email", "first_name": "$elem.author.first_name" }, "body": "$elem.body", "publish_date": "$elem.publish_date", "version": "$elem.version", "author_last_name": "$elem.author_last_name", "parent_id": $elem.parent_id, "article_url": "$elem.article_url" }#if($foreach.hasNext),#end #end ], "version": $inputRoot.version }