与Jackson序列化后无法反序列化Instant

时间:2018-09-27 17:56:39

标签: java json jackson

因此,在Jackson上反序列化Instant变量时遇到问题。基本上,一个类存储这样的Instant:

class Example {
   @JsonProperty("createdAt") 
   private Instant createdAt;

   public example() {
      this.createdAt = Instant.now();
   }
} 

稍后,在单元测试中,我得到了Example类实例的序列化版本,如下所示:

{"createdAt":1538069901.720000000} // content of the payload variable

然后当我尝试反序列化时出现错误:

 private final ObjectMapper JSON_MAPPER = new ObjectMapper();
 Example ex1 = JSON_MAPPER.readValue(payload, Example.class);
  

com.fasterxml.jackson.databind.JsonMappingException:无法构造   java.time.Instant的实例:无double / Double参数   从Number值反序列化的构造函数/工厂方法   (1.53806990172E9)

这样说得通,createdAt的值被视为double(1538069901.720000000),但是我还应该怎么做呢?

0 个答案:

没有答案