猿问

Jackson反序列化SNS消息错误MismatchedInputException

我正在编写一个通过 SNS HTTP 请求处理来自 Amazon Simple Email Service 的回调的功能。我想将亚马逊提供的消息解析为本地对象结构。问题是 SNS 将 JSON 消息包装成字符串,并且 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;

}

我正在寻找某种方法来告诉 JacksonMessage应该从字符串中提取并视为普通的 JSON。


编辑


反序列化


private MessageWrapper deserializeMessage(String message) throws IOException {

    return new ObjectMapper().readValue(message, MessageWrapper.class);

}


叮当猫咪
浏览 446回答 2
2回答

天涯尽头无女友

我认为要解决这个问题,您需要一个用于类Notification中字段的自定义反序列化器MessageWrapper以及一个用于类Mail中字段的自定义反序列化器Notification,如下所示:public class NotificationDeserializer extends JsonDeserializer<Notification> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Notification deserialize(JsonParser p, DeserializationContext ctxt)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws IOException, JsonProcessingException {&nbsp; &nbsp; &nbsp; &nbsp; String text = p.getText();&nbsp; &nbsp; &nbsp; &nbsp; return new ObjectMapper().readValue(text, Notification.class);&nbsp; &nbsp; }}public class MailDeserializer extends JsonDeserializer<Mail> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Mail deserialize(JsonParser p, DeserializationContext ctxt)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws IOException, JsonProcessingException {&nbsp; &nbsp; &nbsp; &nbsp; String text = p.getText();&nbsp; &nbsp; &nbsp; &nbsp; return new ObjectMapper().readValue(text, Mail.class);&nbsp;&nbsp; &nbsp; }}在您的类上添加一些注释,如下所示:@Data@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)public class MessageWrapper {&nbsp; &nbsp; private String type;&nbsp; &nbsp; private String messageId;&nbsp; &nbsp; private String topicArn;&nbsp; &nbsp; @JsonDeserialize(using = NotificationDeserializer.class)&nbsp; &nbsp; private Notification message;&nbsp; &nbsp; private Date timestamp;&nbsp; &nbsp; private String signatureVersion;&nbsp; &nbsp; private String signature;&nbsp; &nbsp; private String signingCertURL;&nbsp; &nbsp; private String unsubscribeURL;}@Datapublic class Notification {&nbsp; &nbsp; private String notificationType;&nbsp; &nbsp; @JsonDeserialize(using = MailDeserializer.class)&nbsp; &nbsp; private Mail mail;}@Datapublic class Mail {&nbsp; &nbsp; private String messageId;&nbsp; &nbsp; private String source;&nbsp; &nbsp; private String sourceArn;&nbsp; &nbsp; private String sourceIp;&nbsp; &nbsp; private String sendingAccountId;&nbsp; &nbsp; private String[] destination;}编辑1实际上并不MailDeserializer需要。独自NotificationDeserializer解决这个问题。编辑2在自定义解串器中使用新的ObjectMapper是必须的。

慕雪6442864

messageproperty 的类型为 type Notification,但Jackson预期JSON Object不是string value。在这种情况下,您可以创建自定义反序列化器或通过某种环回实现来实现通用解决方案。如果给定的有效负载不是 aJSON Object将其读取为 aString并使用 this 再次调用反序列化String。为了避免StackOverflowError您需要使用另一个实例ObjectMapper或使用BeanDeserializerModifier保留BeanDeserializer实例并在遇到的地方使用它JSON Object。简单的例子如下所示:import com.fasterxml.jackson.core.JsonParser;import com.fasterxml.jackson.core.JsonToken;import com.fasterxml.jackson.databind.BeanDescription;import com.fasterxml.jackson.databind.DeserializationConfig;import com.fasterxml.jackson.databind.DeserializationContext;import com.fasterxml.jackson.databind.DeserializationFeature;import com.fasterxml.jackson.databind.JsonDeserializer;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.PropertyNamingStrategy;import com.fasterxml.jackson.databind.annotation.JsonNaming;import com.fasterxml.jackson.databind.deser.BeanDeserializer;import com.fasterxml.jackson.databind.deser.BeanDeserializerBase;import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;import com.fasterxml.jackson.databind.module.SimpleModule;import com.fasterxml.jackson.databind.node.TextNode;import lombok.Data;import lombok.ToString;import java.io.File;import java.io.IOException;import java.util.Collections;import java.util.Date;import java.util.Objects;import java.util.Set;public class JsonApp {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; File jsonFile = new File("./resource/test.json").getAbsoluteFile();&nbsp; &nbsp; &nbsp; &nbsp; SimpleModule loopBackModule = new SimpleModule();&nbsp; &nbsp; &nbsp; &nbsp; loopBackModule.setDeserializerModifier(new LoopBackBeanDeserializerModifier(Collections.singleton(Notification.class)));&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);&nbsp; &nbsp; &nbsp; &nbsp; mapper.registerModule(loopBackModule);&nbsp; &nbsp; &nbsp; &nbsp; MessageWrapper wrapper = mapper.readValue(jsonFile, MessageWrapper.class);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(wrapper.getMessage());&nbsp; &nbsp; }}class LoopBackBeanDeserializerModifier extends BeanDeserializerModifier {&nbsp; &nbsp; private final Set<Class> allowedClasses;&nbsp; &nbsp; LoopBackBeanDeserializerModifier(Set<Class> allowedClasses) {&nbsp; &nbsp; &nbsp; &nbsp; this.allowedClasses = Objects.requireNonNull(allowedClasses);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {&nbsp; &nbsp; &nbsp; &nbsp; if (allowedClasses.contains(beanDesc.getBeanClass())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new LoopBackBeanDeserializer<>((BeanDeserializerBase) deserializer);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return deserializer;&nbsp; &nbsp; }}class LoopBackBeanDeserializer<T> extends BeanDeserializer {&nbsp; &nbsp; private final BeanDeserializerBase baseDeserializer;&nbsp; &nbsp; protected LoopBackBeanDeserializer(BeanDeserializerBase src) {&nbsp; &nbsp; &nbsp; &nbsp; super(src);&nbsp; &nbsp; &nbsp; &nbsp; this.baseDeserializer = src;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; // if first token is VALUE_STRING we should read it as String and&nbsp; &nbsp; &nbsp; &nbsp; // run deserialization process again based on this String.&nbsp; &nbsp; &nbsp; &nbsp; if (p.currentToken() == JsonToken.VALUE_STRING) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (T) ((ObjectMapper) p.getCodec()).readValue(p.getText(), _valueClass);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // vanilla bean deserialization&nbsp; &nbsp; &nbsp; &nbsp; return (T) baseDeserializer.deserialize(p, ctxt);&nbsp; &nbsp; }}&nbsp;POJO型号是一样的。您只需要列出您期望某些问题和loop-back机制适用于它们的类。
随时随地看视频慕课网APP

相关分类

Java
我要回答