简体   繁体   English

我应该如何修改 AWS DynamoDB Query 通过 AWS API 网关返回给我的响应?

[英]How should I modify the response which is returned to me by AWS DynamoDB Query through AWS API Gateway?

I have built a Query API for AWS DynamoDB through the AWS API Gateway.我通过 AWS API 网关为 AWS DynamoDB 构建了查询 API。 Here's a sample response I get after a successful query:这是我在成功查询后得到的示例响应:

{
   "Count":2,
   "Items":[
      {
         "StoreID":{
            "S":"STR100"
         },
         "OrderID":{
            "S":"1019"
         },
         "Date":{
            "S":"22nd May"
         }
      },
      {
         "StoreID":{
            "S":"STR100"
         },
         "OrderID":{
            "S":"1020"
         },
         "Date":{
            "S":"22nd May"
         }
      }
   ],
   "ScannedCount":2
}

What I want to do is, when the response reaches the API Gateway (before it reaches the user) I want to modify it.我想要做的是,当响应到达 API 网关时(在它到达用户之前)我想修改它。 I would like to do the following changes to the coming response -我想对即将到来的响应进行以下更改-

  • Firstly, remove the ScannedCount value.首先,删除ScannedCount值。 (keep the Count one) (保持Count一)
  • Then remove all the "S": "<value>" : value should appear directly, like "StoreID": "STR100"然后删除所有"S": "<value>" :值应该直接出现,例如"StoreID": "STR100"

So, the modified response should look like:因此,修改后的响应应如下所示:

{
   "Count":2,
   "Items":[
      {
         "StoreID":"STR100",
         "OrderID":"1019",
         "Date":"22nd May"
      },
      {
         "StoreID":"STR100",
         "OrderID":"1020",
         "Date":"22nd May"
      }
   ]
}

I hope you understand my problem - Any help is appreciated!我希望你能理解我的问题 - 任何帮助表示赞赏!
Thanks: :)谢谢: :)

Following the blog article Using Amazon API Gateway as a proxy for DynamoDB按照博客文章使用 Amazon API 网关作为 DynamoDB 的代理

Navigate to Integration Response and expand the 200 response code by choosing the arrow on the left.导航到集成响应并通过选择左侧的箭头展开 200 响应代码。 In the 200 response, expand the Mapping Templates section.在 200 响应中,展开映射模板部分。 In Content-Type choose application/json then choose the pencil icon next to Output Passthrough.在 Content-Type 中选择 application/json 然后选择 Output Passthrough 旁边的铅笔图标。

And for your example the template should look like this:对于您的示例,模板应如下所示:

#set($inputRoot = $input.path('$'))
{
    "Count": "$inputRoot.Count",
    "Items": [
        #foreach($elem in $inputRoot.Items) {
            "StoreID": "$elem.StoreID.S",
            "OrderID": "$elem.OrderID.S",
            "Date": "$elem.Date.S"
        }#if($foreach.hasNext),#end
        #end
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM