I want my LocalDateTimes 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?
JacksonAutoConfigurationcreates anObjectMapperwith theWRITE_DATES_AS_TIMESTAMPSfeature turned off, which returnsLocalDateTimesas ISO-8601 strings. When you provide a customObjectMapperthis default auto-configuration is turned off.This can be solved by, instead of providing a custom
ObjectMapper, providing aJackson2ObjectMapperBuilderCustomizer. This bean will be used byJacksonAutoConfigurationto customize theObjectMapperwhile maintaining the auto-configured behaviour such as turning off theWRITE_DATES_AS_TIMESTAMPSfeature.