Reputation: 3176
We have recently migrated from Wildfly 11 to Wildfly 15 and from Java 8 to Java 11 and noticed a change in how Jackson serializes Date objects. We use Jackson v2.9.8 for object serialization and Spring v5.0.9.
Prior to our upgrade, a date object would be serialized in an ISO format e.g. "2019-11-12" but after the upgrade, the date fields started to appear as Timestamps e.g. "1573516800000'. Has anyone else faced this issue before? Is this something that can be configured in standalone.xml
?
Wildfly 11 Example
Wildfly 15 Example
The field is configured as DATE in MySQL
Example Entity
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;
}
}
EDIT:
java.sql.Date
to java.util.Date
and that has not workedUpvotes: 3
Views: 1005
Reputation: 2165
We had this issue after upgrading our Wildfly application server from 11 to 15, and also our Java from 8 to 11.
Based on @Sofo Gial's accepted answer, the following approach worked for us using Spring 5.0.9.RELEASE / JDK 11 / Wildfly 15.
1) Create a 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) In your context beans configuration XML file add a <mvc:message-converters>
entry between the <mvc:annotation-driven>
tags, and create a bean of the 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" />
Upvotes: 2
Reputation: 703
Although I cannot be sure about your current setup/config, if you configure your ObjectMapper
, you will probably get the expected behavior:
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
}
Upvotes: 4
Reputation: 4969
I don't know why it is behaving that way but I would change the type of the field to java.util.Date
and then use the @Temporal
annotation.
This annotation must be specified for persistent fields or properties of type java.util.Date
and java.util.Calendar
. It may only be specified for fields or properties of these types.
According to the documentation:
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.
Your Rest library will then handle conversion between Java Date and ISO. How to configure that depends on the JSON serializer you are using, it is the default format for Date in Jackson
Upvotes: 3