从 Wildfly 11 升级到 Wildfly 15 时出现日期序列化问题

Date serialization issue while upgrading from Wildfly 11 to Wildfly 15

我们最近从 Wildfly 11 迁移到 Wildfly 15,从 Java 8 迁移到 Java 11,并注意到 Jackson 序列化 Date 对象的方式发生了变化。我们使用 Jackson v2.9.8 进行对象序列化和 Spring v5.0.9.

在我们升级之前,日期对象将以 ISO 格式序列化,例如“2019-11-12”,但升级后,日期字段开始显示为时间戳,例如“1573516800000'。以前有没有其他人遇到过这个问题?这可以在 standalone.xml 中配置吗?

野蝇 11 示例

Wildfly 15 示例

该字段在 MySQL

中配置为 DATE

示例实体

public class Entity implements java.io.Serializable {

  @Id
  @Column(name = "id")
  private Integer id;

  @Column(name = "value_date")
  private java.sql.Date valueDate;

  public java.sql.Date getValueDate() {
    return valueDate;
  }

  public void setValueDate(java.sql.Date valueDate) {
    this.valueDate = valueDate;
  }
}

编辑:

我不知道为什么会这样,但我会将字段类型更改为 java.util.Date,然后使用 @Temporal 注释。

必须为 java.util.Datejava.util.Calendar 类型的持久字段或属性指定此注释。只能为这些类型的字段或属性指定。

根据文档:

Temporal data can have DATE, TIME, or TIMESTAMP precision (ie the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.

您的 Rest 库随后将处理 Java 日期和 ISO 之间的转换。如何配置取决于您使用的 JSON 序列化程序,它是 Jackson

中 Date 的默认格式

虽然我不能确定你当前的 setup/config,但如果你配置你的 ObjectMapper,你可能会得到预期的行为:

@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
    ObjectMapper objectMapper = builder.build();
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    return objectMapper;
}

我们在将 Wildfly 应用程序服务器从 11 升级到 15 以及 Java 从 8 升级到 11 后遇到了这个问题。

基于@Sofo Gial's accepted ,以下方法对我们使用Spring 5.0.9.RELEASE / JDK 11 / Wildfly 15.

有效

1) 创建一个 CustomObjectMapper.java:

package com.mobizio.rest.spring;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class CustomObjectMapper extends ObjectMapper {

    public CustomObjectMapper() {
        super();
        configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }

}

2) 在您的上下文 beans 配置 XML 文件中,在 <mvc:annotation-driven> 标记之间添加一个 <mvc:message-converters> 条目,并创建一个 CustomObjectMapper:

<mvc:annotation-driven>
...
<mvc:message-converters>
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="objectMapper" ref="jacksonObjectMapper" />
    </bean>
</mvc:message-converters>
...
</mvc:annotation-driven>

<bean id="jacksonObjectMapper" class="com.mobizio.rest.spring.CustomObjectMapper" />