Java fasterxml.jackson反序列化LocalDateTime wrongTokenException

时间:2017-10-27 06:11:41

标签: java json spring deserialization fasterxml

  • Java 1.8
  • Spring Boot 1.5.8
  • faster.xml.jackson(jackson-module-parameter-namesjackson-datatype-jdk8jackson-datatype-jsr310)2.9.2

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

@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]

UPDATE' S:

整个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":"stackoverflow","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()

1 个答案:

答案 0 :(得分:3)

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

如果您希望这样做,只需更新序列化部分:

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

您应该通过您的应用程序重用相同的对象映射器,对于读者来说也是如此。两者都是因为线程安全,因为实例相对较轻。

运行此命令,您将看到它正常工作,您将看到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);