通过 API 中的 Dynamodb 代理服务发布新记录时出现 SerializationException

2024-01-12

我正进入(状态

"__type": "com.amazon.coral.service#SerializationException"

作为邮递员和 API 网关测试控制台中的回复

尝试使用 API 代理服务将记录直接发布到 dynamodb。 我指的是这篇 AWS 文章 -https://aws.amazon.com/blogs/compute/using-amazon-api-gateway-as-a-proxy-for-dynamodb/ https://aws.amazon.com/blogs/compute/using-amazon-api-gateway-as-a-proxy-for-dynamodb/

这是我的映射

{ 
    "TableName": "TableNameGoesHere",
    "Item": {
    "id" : "$context.requestId"
    "eventName" : "$input.path('$.eventName')",
    "timestamp" : $input.path('$.timestamp'),
    "answers": "$util.parseJson($input.path('$.answers'))"
    }
}

更新: 我按照要求做了...并且它有效,但现在如果我尝试添加 JSON 对象数组,它会给我上面相同的错误 - 这就是我现在正在尝试做的事情。请帮忙 - 在谷歌上也找不到任何东西

#set($inputRoot = $input.path('$'))
{ 
    "TableName": "Answer",
    "Item": {
    "id": {
            "S": "$context.requestId"
            },
    "eventName": {
            "S": "$input.path('$.eventName')"
            },
    "timestamp" : {
            "N": "$input.path('$.timestamp')"
            },
    "answers": {
            "S": "$input.path('$.answers')"
            },
    "Line": {
    "S" : "[
#foreach($elem in $inputRoot.Line)
    {
      "questionID" : "$elem.questionID",
      "answer" : "$elem.answer"
    }#if($foreach.hasNext),#end

#end
  ]" }
    }
}

为了应对挑战对象数组作为有效负载的一部分。

对于请求有效负载

{
    "emailId": "[email protected] /cdn-cgi/l/email-protection",
    "responses": [
        {
            "question": "q1",
            "answer": "a1"
        },
        {
            "question": "q2",
            "answer": "a2"
        }
    ]
}

模板将是

#set($inputRoot = $input.path('$'))
{
    "TableName": "Customers",
    "Item": {
        "leadId": {
            "S": "$context.requestId"
        },
        "emailId": {
            "S": "$input.path('$.emailId')"
        },
        "responses": {
            "L": [            // List type
            #foreach($elem in $inputRoot.responses) // Loop thru array
                {
                    "M": {        // Map type
                        "answer": {
                            "S": "$elem.answer"
                        },
                        "question": {
                            "S": "$elem.question"
                        }
                    }
                }
                #if($foreach.hasNext),#end
            #end
            ]
        }
    }
}

集成响应模板(与要求类似的结构)

#set($inputRoot = $input.path('$'))
{
  "$results": [
    #foreach($elem in $inputRoot.Items)
    {
      "emailId": "$elem.emailId.S",
      "responses": [
          #foreach($resp in $elem.responses.L)
          {
           "question": "$resp.M.question.S",
           "answer": "$resp.M.answer.S"
          }
          #if($foreach.hasNext),#end
          #end
      ]
    }
    #if($foreach.hasNext),#end
    #end
  ]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 API 中的 Dynamodb 代理服务发布新记录时出现 SerializationException 的相关文章

随机推荐