本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
新闻文章示例(API Gateway 模型和映射模板)
以下部分提供了 API Gateway 中可用于示例新闻文章 API 的模型和映射模板的示例。有关 API Gateway 中模型和映射模板的更多信息,请参阅PetStore 映射模板。
原始数据(新闻文章示例)
以下是新闻文章示例的原始 JSON 数据:
{ "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": "1", "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 }
输入模型(新闻文章示例)
以下是与新闻文章示例的原始 JSON 数据对应的输入模型:
{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "NewsArticleInputModel", "type": "object", "properties": { "count": { "type": "integer" }, "items": { "type": "array", "items": { "type": "object", "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": "string" }, "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" } } }
输入映射模板(新闻文章示例)
以下是与新闻文章示例的原始 JSON 数据对应的输入映射模板:
#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 }
转换后的数据(新闻文章示例)
以下是新闻文章示例的原始 JSON 数据如何转换为输出的示例:
{ "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 }
输出模型(新闻文章示例)
以下是与已转换的 JSON 数据格式对应的输出模型:
{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "NewsArticleOutputModel", "type": "object", "properties": { "count": { "type": "integer" }, "items": { "type": "array", "items": { "type": "object", "properties": { "creation_date": { "type": "string" }, "title": { "type": "string" }, "author": { "type": "string" }, "body": { "type": "string" }, "publish_date": { "type": "string" }, "article_url": { "type": "string" } } } }, "version": { "type": "integer" } } }
输出映射模板(新闻文章示例)
以下是与已转换的 JSON 数据格式对应的输出映射模板。此处的模板变量基于原始 (而非已转换的) JSON 数据格式:
#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 }