I want my LocalDateTime
s to be returned as ISO-8601 strings (e.g. "2020-10-12T10:57:15Z"
) from my Spring REST Controllers. This has worked previously, but now that I'm using a custom Jackson2 ObjectMapper
these dates are instead being returned as arrays: [2020, 10, 12, 10, 57, 15, 200000000]
.
Why is this happening and how can I customize the ObjectMapper
while still returning ISO-8601 dates?
JacksonAutoConfiguration
creates anObjectMapper
with theWRITE_DATES_AS_TIMESTAMPS
feature turned off, which returnsLocalDateTimes
as ISO-8601 strings. When you provide a customObjectMapper
this default auto-configuration is turned off.This can be solved by, instead of providing a custom
ObjectMapper
, providing aJackson2ObjectMapperBuilderCustomizer
. This bean will be used byJacksonAutoConfiguration
to customize theObjectMapper
while maintaining the auto-configured behaviour such as turning off theWRITE_DATES_AS_TIMESTAMPS
feature.