1

我有来自AWS Lambda的JSON响应到AWS API-GATEWAY,如下所示:-

[
   {
       "key1" : "fruit",
       "key2" : "citrus",
       "key3" : {
                  "key31" : "lemon",
                  "key32" : "orange",
                  "key33" : "lime"
                }
   },

   {
       "key1" : "vegetable",
       "key2" : "green",
       "key3" : {
                  "key31" : "spinach",
                  "key32" : "lettuce",
                  "key33" : "cabbage"
                }
   }

]

在从 API Gateway 发送到客户端应用程序之前,我想修改响应中的密钥,如下所示:

[
   {
       "category" : "fruit",
       "subCategory" : "citrus",
       "examples" : {
                  "eg1" : "lemon",
                  "eg2" : "orange",
                  "eg3" : "lime"
                }
   },

   {
       "category" : "vegetable",
       "subCategory" : "green",
       "examples" : {
                  "eg1" : "spinach",
                  "eg2" : "lettuce",
                  "eg33" : "cabbage"
                }
   }

]

AWS ApiGateway 中,我们有映射模板来转换来自 Lambda 的响应并使用Apache Velocity离开 API Gateway 。我正在使用application/json格式来创建映射模板

下面是我为转换编写的代码——

#set($inputRoot = $input.path('$'))
[
    #foreach($elem in $inputRoot)
    {
      "category": "$elem.key1",
      "subCategory": "$elem.key2",
      "examples" : #set($example in $elem.key3) 
                 {
                  "eg1" : "$example.key31",
                  "eg2" : "$example.key32",
                  "eg3" : "$example.key33"
                 }#end
    }#if($foreach.hasNext),#end
#end
]

我在点击它后从 api 网关收到的响应如下 ---

{
  "message": "Internal server error"
}

我还是 API 网关的新手,所以如果有人能提供帮助,那就太好了。

4

0 回答 0