杰克逊 jsr310 中缺少 ZonedDateTimeDeserializer

ZonedDateTimeDeserializer is missing in jackson jsr310

我正在解析 ZonedDateTime,使用如下:

 @JsonSerialize(using = ZonedDateTimeSerializer.class)
 private ZonedDateTime expirationDateTime;

我需要能够正确反序列化这个日期。但是,jackson 没有为此提供反序列化器:

com.fasterxml.jackson.datatype.jsr310.deser

它丢失有什么原因吗?最常见的解决方法是什么?

已更新: 这是我的场景:

我这样创建 ZonedDateTime

ZonedDateTime.of(2017, 1, 1, 1, 1, 1, 1, ZoneOffset.UTC)

然后我像这样序列化包含日期的对象:

public static String toJSON(Object o) {
    ObjectMapper objectMapper = new ObjectMapper();
    StringWriter sWriter = new StringWriter();
    try {
        JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(sWriter);
        objectMapper.writeValue(jsonGenerator, o);
        return sWriter.toString();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

当我尝试将它发送到 Spring MVC 控制器时:

    mockMvc.perform(post("/endpoint/")
            .content(toJSON(myObject))
            .contentType(APPLICATION_JSON))
            .andExpect(status().isOk());

控制器内部的日期对象不同。

之前:2017-01-01T01:01:01.000000001Z

之后:2017-01-01T01:01:01.000000001Z[UTC]

2017-01-01T01:01:01.000000001Z2017-01-01T01:01:01.000000001Z[UTC]这2个值实际上代表了同一个瞬间,所以它们是等价的,可以毫无问题地使用(至少应该 没问题,因为它们代表同一瞬间)。

唯一的细节是杰克逊出于某种原因在反序列化时将 ZoneId 值设置为 "UTC",在这种情况下这是多余的(Z 已经表明偏移量为 "UTC")。但它不应该影响日期值本身。


摆脱这个 [UTC] 部分的一个非常简单的方法是将这个对象转换为 OffsetDateTime(因此它保持 Z 偏移并且不使用 [UTC] 区域)然后再次回到 ZonedDateTime:

ZonedDateTime z = // object with 2017-01-01T01:01:01.000000001Z[UTC] value
z = z.toOffsetDateTime().toZonedDateTime();
System.out.println(z); // 2017-01-01T01:01:01.000000001Z

之后,z变量的值将是2017-01-01T01:01:01.000000001Z(没有[UTC]部分)。

但这当然不理想,因为您必须为所有日期手动执行此操作。更好的方法是编写一个自定义解串器(通过扩展 com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer),在 UTC:

时不设置时区
public class CustomZonedDateTimeDeserializer extends InstantDeserializer<ZonedDateTime> {
    public CustomZonedDateTimeDeserializer() {
        // most parameters are the same used by InstantDeserializer
        super(ZonedDateTime.class,
              DateTimeFormatter.ISO_ZONED_DATE_TIME,
              ZonedDateTime::from,
              // when zone id is "UTC", use the ZoneOffset.UTC constant instead of the zoneId object
              a -> ZonedDateTime.ofInstant(Instant.ofEpochMilli(a.value), a.zoneId.getId().equals("UTC") ? ZoneOffset.UTC : a.zoneId),
              // when zone id is "UTC", use the ZoneOffset.UTC constant instead of the zoneId object
              a -> ZonedDateTime.ofInstant(Instant.ofEpochSecond(a.integer, a.fraction), a.zoneId.getId().equals("UTC") ? ZoneOffset.UTC : a.zoneId),
              // the same is equals to InstantDeserializer
              ZonedDateTime::withZoneSameInstant, false);
    }
}

然后你必须注册这个解串器。如果你使用ObjectMapper,你需要将这个添加到JavaTimeModule:

ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule module = new JavaTimeModule();
// add my custom deserializer (this will affect all ZonedDateTime deserialization)
module.addDeserializer(ZonedDateTime.class, new CustomZonedDateTimeDeserializer());
objectMapper.registerModule(module);

如果你在Spring中配置它,配置将是这样的(未测试):

<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" id="pnxObjectMapper">
    <property name="deserializersByType">
        <map key-type="java.lang.Class">
            <entry>
                <key>
                    <value>java.time.ZonedDateTime</value>
                </key>
                <bean class="your.app.CustomZonedDateTimeDeserializer">
                </bean>
            </entry>
        </map>
    </property>
</bean>

我用这个:

        JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(DateTimeFormatter.ISO_DATE_TIME));
    javaTimeModule.addDeserializer(ZonedDateTime.class, InstantDeserializer.ZONED_DATE_TIME);