繁体   English   中英

如何正确解析SES在Java中弹回JSON SNS通知

[英]How correctly parse SES bounced JSON SNS Notification in Java

我从SNS主题收到JSON,我认为这是不正确的

{
   "Type":"Notification",
   "MessageId":"message-id-is-this",
   "TopicArn":"bouncer.topic.name.here",
   "Message":"{\"notificationType\":\"Bounce\",\"bounce\":{\"bounceType\":\"Permanent\",\"bounceSubType\":\"General\",\"bouncedRecipients\":[{\"emailAddress\":\"bounce@simulator.amazonses.com\",\"action\":\"failed\",\"status\":\"5.1.1\",\"diagnosticCode\":\"smtp; 550 5.1.1 user unknown\"}],\"timestamp\":\"2017-04-24T12:58:05.716Z\",\"feedbackId\":\"feedback.id.is.here\",\"remoteMtaIp\":\"192.168.10.1\",\"reportingMTA\":\"dsn; smtp.link.here\"},\"mail\":{\"timestamp\":\"2017-04-24T12:58:05.000Z\",\"source\":\"senderEmail@domainname.com\",\"sourceArn\":\"arn:aws:ses:us-east-1:someid:identity/some@domain.org\",\"sourceIp\":\"127.0.0.1\",\"sendingAccountId\":\"sending.account.id.is.this\",\"messageId\":\"message-id-is-this\",\"destination\":[\"bounce@simulator.amazonses.com\"]}}",
   "Timestamp":"2017-04-24T12:58:05.757Z",
   "SignatureVersion":"1",
   "Signature":"signature.link",
   "SigningCertURL":"certificate.link.here",
   "UnsubscribeURL":"un.subscribe.link"
}

问题在于“Message”属性不是持有对象,而是指对象的字符串

包含

"Message":"{\"key\":\"value\"}"

代替

"Message":{"key":"value"}"

因此没有映射到Message类

暂时我通过接收到字符串变量然后转换它来解决这个问题

private String Message;
private Message objMessage;

接着

Notification noti = toObject(jsonString, Notification.class);
Message msg = toObject(noti.getMessage(), Message.class);
noti.setObjMessage(msg);

为了转换,我使用的是ObjectMapper.readValue(...)

解决这个问题的正确方法是什么?

这种格式是正确的。

循环中有两个独立的服务,SES和SNS。

外部结构是SNS通知 - SNS使用的通用结构可以提供SNS提供的任何内容

它包含一个Message属性,其值始终是一个字符串,因为这是SNS提供的消息类型 -​​ 字符串。 不是对象。 SNS没有意识到Message属性的值是任何类型的对象。 它可以是任何东西,只要它是有效的UTF-8,SNS就不在乎了。

要将对象作为字符串传递,必须将其序列化......并且内部序列化也恰好是JSON。

所以Message是嵌套的JSON-in-JSON。

这就是它应该是什么样子。

当外部对象被序列化时,必须对内部的保留JSON字符进行转义,如此处所示。 在第一次反序列化之后,您正好拥有SES发送给您的内容 - 一个JSON字符串。

然后,您需要反序列化生成的字符串以获取对象。

我不认为你做错了。 如果你是,那么多年来我一直在做错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM