Search code examples
spring-bootjacksondeserialization

Deserialize Date object with Jackson in Spring boot


My data object returns data as 1604571544010, and I need something more readable.

I tried so many things and nothing seems work, I suspect that there is a problem with Jackson or the spring version.

I tried to set the data format in the application.properties

Also I ttried all global configurations from baeldung

Pom.xml

<jackson.version>2.11.3</jackson.version>
<jackson.databind.version>2.11.3</jackson.databind.version>
<jackson.mapper.version>1.9.13</jackson.mapper.version>
<spring.boot.version>2.1.17.RELEASE</spring.boot.version>
<spring.version>5.1.18.RELEASE</spring.version>

WebConfig

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder()
{
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    return new Jackson2ObjectMapperBuilder().indentOutput(true).dateFormat(sdf);
}

DTO

@JsonProperty
public Date getStartDateTime()
{
    return startDateTime;
}

Solution

  • the only global setting that worked for me is ObjectMapper

       @Bean
            @Primary
            public ObjectMapper objectMapper()
            {
                final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                final ObjectMapper mapper = new ObjectMapper();
                mapper.setDateFormat(sdf);
                mapper.enable(SerializationFeature.INDENT_OUTPUT);
                return mapper;
            }