java - SNSEvent 的 GSON 序列化导致 lambda 需要大写单词的小写字段
问题描述
我有一个 lambda 函数,它采用 SNSEvent( http://javadox.com/com.amazonaws/aws-lambda-java-events/1.1.0/com/amazonaws/services/lambda/runtime/events/SNSEvent.html ) 作为输入,然后进行处理。为了编写一些集成测试用例,我想使用 lambda.invoke 调用这个带有 SNSEvent 序列化的 lambda,其中消息将是字符串。
所以,为此,我正在做 GSON.toJson([object of SNSEvent type]) 并且从中得到一个字符串。但问题是,当我使用这个字符串调用 lambda 时,我得到一个异常,这个字符串的反序列化失败。
例如。GSON.toJson 正在反序列化为:
{
"records": [
{
"eventSource": "aws:sns",
"eventVersion": "1.0",
"eventSubscriptionArn": "arn:aws:sns:us-west-2:{{{accountId}}}:ExampleTopic",
"sns": {
"type": "Notification",
"messageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"topicArn": "arn:aws:sns:us-west-2:{{accountId}}:ExampleTopic",
"subject": "example subject",
"message": "example message",
"timestamp": "1970-01-01T00:00:00.000Z",
"signatureVersion": "1",
"signature": "EXAMPLE",
"signingCertUrl": "EXAMPLE",
"unsubscribeUrl": "EXAMPLE",
"messageAttributes": {
"test": {
"type": "String",
"value": "TestString"
},
"testBinary": {
"type": "Binary",
"value": "TestBinary"
}
}
}
}
]
}
而实际上,它想要的是(大写字母):
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-west-2:{{{accountId}}}:ExampleTopic",
"Sns": {
"Type": "Notification",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"TopicArn": "arn:aws:sns:us-west-2:{{accountId}}:ExampleTopic",
"Subject": "example subject",
"Message": "example message",
"Timestamp": "1970-01-01T00:00:00.000Z",
"SignatureVersion": "1",
"Signature": "EXAMPLE",
"SigningCertUrl": "EXAMPLE",
"UnsubscribeUrl": "EXAMPLE",
"MessageAttributes": {
"Test": {
"Type": "String",
"Value": "TestString"
},
"TestBinary": {
"Type": "Binary",
"Value": "TestBinary"
}
}
}
}
]
}
知道为什么会这样吗?我该如何解决?在调用 lambda 时我是否应该使用其他对象而不是 SNSEvent。
解决方案
推荐阅读
- python - 尝试从不同链接抓取信息时,旧元素参考元素未附加到页面文档
- microstrategy - 如何在 MSTR 中依次添加 2 个属性?
- python - 尝试使用插入排序对字典列表及其子项的值进行排序(python)
- arrays - Jolt 将元素从子数组传递到父数组
- amazon-dynamodb - 查询范围内的 DynamoDb 时间戳数据
- python - plt.plot (rr,xlogistic) 在程序中的什么位置?我是 Python 新手
- c - 递归和遍历二叉树
- php - 如何获取在 Wordpress 帖子中上传的文件的附件 ID?
- angular - @output 在 Angular 10 中没有发出结果
- c++ - C++ 手动展开(条件和)比常规代码慢,期待 AVX 矢量化