通过API中的Dynamodb代理服务发布新记录时出现SerializationException [英] SerializationException in posting new Records via Dynamodb Proxy Service in API

查看:97
本文介绍了通过API中的Dynamodb代理服务发布新记录时出现SerializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到

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

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

as a reply in postman & in test console in API Gateway

尝试使用API​​代理服务将记录直接发布到dynamodb. 我指的是这篇AWS文章-

Trying to post a record directly to dynamodb using API Proxy Services.. I am referring this AWS Article - 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对象数组,它会给我上述相同的错误-这是我现在正在尝试的操作.请帮忙-也无法在Google上找到任何内容

Update: I did as asked ... and it worked but now if I try to add a Array of JSON Objects it gives me the above same error - here's what I am trying to do now. Please help - Couldnt find anything on google as well

#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
  ]" }
    }
}

推荐答案

要解决将对象数组作为有效负载的一部分的挑战.

To address the challenge of having Array of Objects as part of the payload.

用于请求有效载荷

{
    "emailId": "v@a.com",
    "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
            ]
        }
    }
}

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

Integration Response Template (similar structure as request)

#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
  ]
}

这篇关于通过API中的Dynamodb代理服务发布新记录时出现SerializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆