I'm using Spring Boot 2.1.4 and Spring Data Jest with ElasticSearch. I was initially using Java Date for some properties with the following annotation:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
This is saved into ElasticSearch as follows:
"creationDate": "2019-04-10T14:49:05.672+0000"
Now, I am in the process of migrating from Date to LocalDateTime and ZonedDateTime. When the data is now saved to ElasticSearch, I get the following attribute saved:
"creationDate": {
"dayOfYear": 123,
"dayOfWeek": "FRIDAY",
"month": "MAY",
"dayOfMonth": 3,
"year": 2019,
"monthValue": 5,
"hour": 11,
"minute": 54,
"second": 12,
"nano": 238000000,
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
}
},
What do I need to do to change it so I get the same ElasticSearch data format as before for LocalDateTime and ZonedDateTime?
I have tried the following:
Customising the object mapper as follows:
public class CustomEntityMapper implements EntityMapper { private final ObjectMapper objectMapper; public CustomEntityMapper(ObjectMapper objectMapper) { this.objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); objectMapper.registerModule(new CustomGeoModule()); objectMapper.registerModule(new JavaTimeModule()); } @Override public String mapToString(Object object) throws IOException { return objectMapper.writeValueAsString(object); } @Override public <T> T mapToObject(String source, Class<T> clazz) throws IOException { return objectMapper.readValue(source, clazz); } }
Adding the following to object mapper:
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
Any help or pointers where I'm going wrong would be appreciated.