首页 > 解决方案 > 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。

标签: javaamazon-web-servicesaws-lambdaamazon-sns

解决方案


推荐阅读