Java 8 date time types serialized as object with Spring Boot

I started having this issue in the upgrade of spring boot from 2.3.7 to 2.5.1.

If you have an ObjectMapper @Bean defined, then you'll want to register the time module with it.

@Bean
public ObjectMapper defaultMapper() {
    ObjectMapper objectMapper = new ObjectMapper(); 
    objectMapper.registerModule(new JavaTimeModule()); 
    return objectMapper;
}

A lot of the time, coders will just create a "new ObjectMapper()" when using jackson serialization so keep an eye out for using the vanilla mapper, rather than autowiring a pre-configured default that has the time module registered.

As mentioned, you'll need the jackson-datatype-jsr310, but this is included in spring boot as a managed version.

If you don't define an object mapper bean manually, then spring boot should automatically provide one with the time module registered.


According to How to customize ObjectMapper :

Any beans of type com.fasterxml.jackson.databind.Module will be automatically registered with the auto-configured Jackson2ObjectMapperBuilder and applied to any ObjectMapper instances that it creates. This provides a global mechanism for contributing custom modules when you add new features to your application.

Just adding the dependancy is not enough, you have to declare a @Bean of you module like follow:

@Bean
public Module dateTimeModule(){
    return new JavaTimeModule();
}

Plus jackson-datatype-jsr310 module is deprecated you should use JavaTimeModule instead.