2016-11-10 48 views
0

我越来越SerializationException在通过Dynamodb代理服务的API发布新唱片

"__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/

这里是我的映射

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

更新: 我按照要求......并且它的工作,但现在如果我尝试添加一个JSON对象数组,它给了我上述相同的错误 - 这是我现在要做的。请帮助 - 在Google上找不到任何东西

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

回答

0

为了解决其数组对象作为有效载荷的一部分的挑战。

对于请求负载

{ 
    "emailId": "[email protected]", 
    "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 
    ] 
} 
2

您的映射模板与DynamoDB格式不匹配。它应该是这样的,

{ 
    "TableName": "Comments", 
    "Item": { 
     "commentId": { 
      "S": "$context.requestId" 
     }, 
     "pageId": { 
      "S": "$input.path('$.pageId')" 
      }, 
     "userName": { 
      "S": "$input.path('$.userName')" 
     }, 
     "message": { 
      "S": "$input.path('$.message')" 
     } 
    } 
} 
+0

好的...我明白了......但是我想在数据库字段中添加一个Json对象 - 你能否在aws文档中提供一个参考。 –

+0

它的工作,但我想添加一个Json对象的数组 - 为此我指的是http://docs.aws.amazon.com/apigateway/latest/developerguide/example-invoice.html#example-invoice - 输入映射,但我没有得到aws文档的API数据类型 –

+0

@mac模具 看看我的回应低于 https://stackoverflow.com/a/44320439/1871718 –

0

这是我的解决方案:

"TableName": "Comments", 
"Item": { 
    "commentId": { 
     "S": "$context.requestId" 
    }, 
    "pageId": { 
     "S": "$input.path('$.pageId')" 
     }, 
    "userName": { 
     "S": "$input.path('$.userName')" 
    }, 
    "message": { 
     "S": "$input.path('$.message')" 
    } 
} 
相关问题