AWS API Gateway作为dynamo DB HTTP的代理获取映射模板

时间:2018-01-14 03:42:08

标签: amazon-web-services amazon-dynamodb aws-api-gateway

我有一个API网关,可以访问存储在dynamo DB中的表。 存储的表看起来像JSON,如下所示

{
  "photos": {
    "page": 1,
    "pages": "1234",
    "perpage": 100,
    "photo": [
      {
        "farm": 1,
        "id": "12345678901",
        "isfamily": 0,
        "isfriend": 0,
        "ispublic": 1,
        "owner": "23456789@A12",
        "secret": "abc123d456",
        "server": "1234",
        "title": "Sample photo 1"
      },
      {
        "farm": 2,
        "id": "23456789012",
        "isfamily": 0,
        "isfriend": 0,
        "ispublic": 1,
        "owner": "34567890@B23",
        "secret": "bcd234e567",
        "server": "2345",
        "title": "Sample photo 2"
      }
    ],
    "total": "123398"
  },
  "srini": "srini"
}

使用out积分响应映射模板,我得到如下所示的表

  {  
 "Count": 1,   "Items": [
            {
              "photos": {
                "M": {
                  "photo": {
                    "L": [
                      {
                        "M": {
                          "owner": {
                            "S": "23456789@A12"
                          },
                          "server": {
                            "S": "1234"
                          },
                          "ispublic": {
                            "N": "1"
                          },
                          "isfriend": {
                            "N": "0"
                          },
                          "farm": {
                            "N": "1"
                          },
                          "id": {
                            "S": "12345678901"
                          },
                          "secret": {
                            "S": "abc123d456"
                          },
                          "title": {
                            "S": "Sample photo 1"
                          },
                          "isfamily": {
                            "N": "0"
                          }
                        }
                      },
                      {
                        "M": {
                          "owner": {
                            "S": "34567890@B23"
                          },
                          "server": {
                            "S": "2345"
                          },
                          "ispublic": {
                            "N": "1"
                          },
                          "isfriend": {
                            "N": "0"
                          },
                          "farm": {
                            "N": "2"
                          },
                          "id": {
                            "S": "23456789012"
                          },
                          "secret": {
                            "S": "bcd234e567"
                          },
                          "title": {
                            "S": "Sample photo 2"
                          },
                          "isfamily": {
                            "N": "0"
                          }
                        }
                      }
                    ]
                  },
                  "perpage": {
                    "N": "100"
                  },
                  "total": {
                    "S": "123398"
                  },
                  "pages": {
                    "S": "1234"
                  },
                  "page": {
                    "N": "1"
                  }
                }
              },
              "srini": {
                "S": "srini"
              }
            }   ],   "ScannedCount": 1 
}

我正在尝试以JSON格式检索,以便Web客户端以JSON格式从Dynamo中获取表。我尝试编写的映射模板如下所示

#set($inputRoot = $input.path('$'))
{
        #foreach($elem in $inputRoot.Items) {
                "srini": "$elem.srini.S",
                "pages": "$elem.photos.pages.S",

                 #foreach($elemnext in $elem.photos.photo) {
                     "id": "$elemnext.id.S"

                }#if($foreach.hasNext),#end
               #end

            }#if($foreach.hasNext),#end
        #end

}

我只能检索srini,如下所示

Response Body
{
         {
                "srini": "srini",
                "pages": ""
                             }            
}

所有其他数据都没有被检索,编写地图模板的正确方法是什么,任何人都可以告诉我吗?

1 个答案:

答案 0 :(得分:1)

#set($inputRoot = $input.path('$'))
{
        #foreach($elem in $inputRoot.Items) {
                "srini": "$elem.srini.S",
                "pages": "$elem.photos.M.pages.S",
                 #foreach($elemnext in $elem.photos.M.photo.L)
                 {
                    "id": "$elemnext.M.id.S"
                 } #if($foreach.hasNext),#end
                #end

            }#if($foreach.hasNext),#end
        #end

}