【发布时间】:2019-10-02 15:06:37
【问题描述】:
我正在编写一个通过 SNS HTTP 请求处理来自 Amazon Simple Email Service 的回调的功能。我想将亚马逊提供的消息解析为本地对象结构。问题是 SNS 将 JSON 消息包装成 String 并且 Jackson 无法解析它。我收到一个错误:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `xxx.email.domain.aws.ses.Notification` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"notificationType":"Delivery","mail":{"timestamp":"2019-10-02T14:43:14.570Z" ... next values of the message ... }}')
来自 SNS 的整个消息如下所示:
{
"Type" : "Notification",
"MessageId" : "4944xxxx-711d-57d4-91b8-8215cxxxxx",
"TopicArn" : "arn:aws:sns:eu-west-1:...",
"Message" : "{\"notificationType\":\"Delivery\",\"mail\":{\"timestamp\":\"2019-10-02T14:43:14.570Z\", ... next values of the message ... },\"delivery\":{\"timestamp\":\"2019-10-02T14:43:16.030Z\", ... next values of the message ... }}",
"Timestamp" : "2019-10-02T14:43:16.062Z",
"SignatureVersion" : "1",
"Signature" : "signature base64",
"SigningCertURL" : "cert url",
"UnsubscribeURL" : "unsubscribe url"
}
我的实际本地结构如下所示:
@Data
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class MessageWrapper {
private String type;
private String messageId;
private String topicArn;
private Notification message;
private Date timestamp;
private String signatureVersion;
private String signature;
private String signingCertURL;
private String unsubscribeURL;
}
@Data
public class Notification {
private String notificationType;
private Mail mail;
}
@Data
public class Mail {
private String messageId;
private String source;
private String sourceArn;
private String sourceIp;
private String sendingAccountId;
private String[] destination;
}
我正在寻找某种方法来告诉 Jackson Message 应该从字符串中提取并作为普通 JSON 对待。
编辑
反序列化
private MessageWrapper deserializeMessage(String message) throws IOException {
return new ObjectMapper().readValue(message, MessageWrapper.class);
}
【问题讨论】:
-
您不需要为您的所有 3 个类添加相同的注释
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)吗? -
@Ivan 即使我将这个注释添加到所有这些,我仍然得到 MismatchedInputException
-
能否添加通过 Jackson 解析的代码 sn-p?
-
@mcarlin 添加在末尾span>
标签: java amazon-web-services jackson aws-sdk amazon-sns