Java fasterxml.jackson 反序列化 LocalDateTime wrongTokenException

Java fasterxml.jackson deserialize LocalDateTime wrongTokenException

我有一个带有时间戳的对象。

@JsonProperty("timestamp")
private LocalDateTime timestamp;  

我需要反序列化的LocalDateTime戳是:

{ 
   "year":2017,
   "month":"OCTOBER",
   "dayOfMonth":27,
   "dayOfWeek":"FRIDAY",
   "dayOfYear":300,
   "monthValue":10,
   "nano":460000000,
   "hour":4,
   "minute":47,
   "second":29,
   "chronology":{  
      "calendarType":"iso8601",
      "id":"ISO"
   }
}

我尝试反序列化对象:

MyObject myObject=
         new ObjectMapper()
             .findAndRegisterModules()
             .treeToValue(jsonPayload, MyObject.class);

应用程序在此调用时冻结,如果我中断调用,我会收到以下错误消息:

Error while stopping the container:
java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(Lcom/fasterxml/jackson/core/JsonParser;Ljava/lang/Class;Lcom/fasterxml/jackson/core/JsonToken;Ljava/lang/String;)Lcom/fasterxml/jackson/databind/JsonMappingException;
        at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:138) ~[jackson-datatype-jsr310-2.9.2.jar!/:2.9.2]
        at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:39) ~[jackson-datatype-jsr310-2.9.2.jar!/:2.9.2]
        at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:504) ~[jackson-databind-2.8.10.jar!/:2.8.10]
        at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:111) ~[jackson-databind-2.8.10.jar!/:2.8.10]
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276) ~[jackson-databind-2.8.10.jar!/:2.8.10]
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140) ~[jackson-databind-2.8.10.jar!/:2.8.10]
        at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3786) ~[jackson-databind-2.8.10.jar!/:2.8.10]
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2115) ~[jackson-databind-2.8.10.jar!/:2.8.10]
        at com.fasterxml.jackson.databind.ObjectMapper.treeToValue(ObjectMapper.java:2612) ~[jackson-databind-2.8.10.jar!/:2.8.10]

更新:

整个JSON是:

{"valid":true,"version":"0.0.1",
"timestamp":{"year":2017,"month":"OCTOBER","dayOfMonth":27,"dayOfWeek":"FRIDAY","dayOfYear":300,"monthValue":10,"nano":460000000,"hour":4,"minute":47,"second":29,"chronology":{"calendarType":"iso8601","id":"ISO"}},
"tenant":"Whosebug","uid":"10fa132f-2c92-4fa5-bcc2-ee6023281503"})

序列化为:

String jsonString = new ObjectMapper().writeValueAsString(messageIndex);

对象属性:

@JsonProperty("version")
private String version;

@JsonProperty("timestamp")
private LocalDateTime timestamp;

@JsonProperty("tenant")
private String tenant;

@JsonProperty("dataSampleUid")
private UUID uid;

private boolean isValid;

LocalDateTime 设置为:

LocalDateTime.now()

序列化时您没有注册任何模块,尤其是 JavaTimeModule,因此 LocalDateTime 将像任何其他对象一样被序列化(打印所有可访问的字段)。但是,在反序列化部分,您注册了此模块,他们期望 LocalDateTime (by default an array) 的特定格式。

如果你想让它工作,只需更新序列化部分:

String jsonString = new ObjectMapper().findAndRegisterModules().writeValueAsString(messageIndex);

您应该在您的应用程序中重复使用同一个对象映射器,reader 也是如此。既因为线程安全,也因为实例相对轻量级。

运行 这样你就会看到它工作正常,你会看到 LocalDateTime 序列化值的差异。

ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
MyObject data = new MyObject();
data.setTimestamp(LocalDateTime.now());
String result = mapper.writeValueAsString(data);
System.out.println(result);
JsonNode tree = mapper.reader().readTree(result);
mapper.treeToValue(tree, MyObject.class);