2017-04-24 46 views
0

我从SNS的主题,我相信接受JSON反弹JSON SNS通知是不正确的如何正确地分析SES在Java中

{ 
    "Type":"Notification", 
    "MessageId":"message-id-is-this", 
    "TopicArn":"bouncer.topic.name.here", 
    "Message":"{\"notificationType\":\"Bounce\",\"bounce\":{\"bounceType\":\"Permanent\",\"bounceSubType\":\"General\",\"bouncedRecipients\":[{\"emailAddress\":\"[email protected]\",\"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\":\"[email protected]\",\"sourceArn\":\"arn:aws:ses:us-east-1:someid:identity/[email protected]\",\"sourceIp\":\"127.0.0.1\",\"sendingAccountId\":\"sending.account.id.is.this\",\"messageId\":\"message-id-is-this\",\"destination\":[\"[email protected]\"]}}", 
    "Timestamp":"2017-04-24T12:58:05.757Z", 
    "SignatureVersion":"1", 
    "Signature":"signature.link", 
    "SigningCertURL":"certificate.link.here", 
    "UnsubscribeURL":"un.subscribe.link" 
} 

问题是与“信息”的属性,它的不是举行一个对象,是参照对象的字符串

包含

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

代替

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

因此没有被映射到消息类

暂时我通过接收到字符串变量解决了这个问题,并然后将其转换

private String Message; 
private Message objMessage; 

然后

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

用于转化,我正在使用ObjectMapper.readValue(...)

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

回答

1

这种格式是正确的。

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

外部结构是SNS通知 - SNS使用的通用结构提供任何东西 SNS提供。

它包含一个Message属性,其值为总是一个字符串,因为这是SNS传递的一种消息 - 字符串。不是对象。 SNS没有意识到Message属性的值是任何类型的对象。它可以是任何东西,只要它是有效的UTF-8,SNS不关心。

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

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

这就是它应该看起来像。

当外部对象被序列化时,里面的保留JSON字符必须被转义,如下所示。在第一次反序列化之后,您完全知道SES向您发送了什么 - 一个JSON字符串。

然后您需要反序列化结果字符串才能获取对象。

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