CodexBloom - Programming Q&A Platform

Java 17: implementing ObjectMapper not Serializing LocalDateTime Correctly in Spring Boot App

👀 Views: 1 💬 Answers: 1 📅 Created: 2025-06-13
java spring-boot jackson Java

I'm stuck on something that should probably be simple. I've tried everything I can think of but I've been banging my head against this for hours. I'm experiencing a question where the `ObjectMapper` from the Jackson library is not serializing `LocalDateTime` fields as expected in my Spring Boot application. Despite configuring the `ObjectMapper` to handle Java 8 types, the output JSON for `LocalDateTime` is resulting in an unexpected format. Here's a snippet of my `application.properties`: ```properties spring.jackson.serialization.write-dates-as-timestamps=false ``` I also tried manually configuring the `ObjectMapper` bean in my configuration class: ```java import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JacksonConfig { @Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); return mapper; } } ``` However, when I run my application, and a `LocalDateTime` property is serialized, it still appears as a timestamp instead of a proper ISO-8601 string format. For example, instead of: ```json "dateTime":"2023-10-01T14:30:00" ``` I get: ```json "dateTime":1696165200000 ``` I’ve also ensured that I am using the latest version of Jackson (`2.14.0`) and Spring Boot (`2.7.5`). Is there something I'm missing in my configuration, or is it possible that another part of my application is overriding these settings? Any guidance would be appreciated! Has anyone else encountered this? This is for a application running on Ubuntu 22.04. Is there a simpler solution I'm overlooking? My development environment is Debian. Any pointers in the right direction?