spring-boot: spring.jackson.date-format configuration does not affect serialisation of Joda DateTime instances

I have breakpoints set in both: MappingJackson2HttpMessageConverterConfiguration.mappingJackson2HttpMessageConverter JacksonObjectMapperConfiguration.jacksonObjectMapper

I can see that there is an ObjectMapper bean being created because jacksonObjectMapper is never called and thus MappingJackson2HttpMessageConverterConfiguration uses this other ObjectMapper and not the one that I’m trying to configure. How do I tell what is creating that bean? From that logs, my guess is that it is HypermediaSupportBeanDefinitionRegistrar.registerBeanDefinitions that is doing it from:

        <dependency>
            <groupId>org.springframework.hateoas</groupId>
            <artifactId>spring-hateoas</artifactId>
        </dependency>

If I put a breakpoint there it is definitely called before MappingJackson2HttpMessageConverterConfiguration.mappingJackson2HttpMessageConverter but it is so hard to tell if this is the root of my problem. I can’t remove that dependency because it is so pervasively used in my project that it would take me hours to refactor.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 22 (21 by maintainers)

Most upvoted comments

The configuration of write-dates-as-timestamps works correctly as far as I can tell, and it affects the serialisation of both java.util.Date and org.joda.time.DateTime instances. The problem is that when write-dates-as-timestamps is false spring.jackson.date-format only affects the serialisation of java.util.Date instances.

This configuration:

spring:
  jackson:
    date-format: "YYYY-MM-dd"
    serialization:
      write_dates_as_timestamps: true

Gives me this output:

{"date":1421343027885,"dateTime":1421343027895}

Note that both the java.util.Date and org.joda.time.DateTime instances have been serialised as timestamps.

On the other hand this configuration:

spring:
  jackson:
    date-format: "YYYY-MM-dd"
    serialization:
      write_dates_as_timestamps: false

Gives me this output:

{"date":"2015-01-15","dateTime":"2015-01-15T17:31:28.884Z"}

Note that both are now being formatted as strings but that only java.util.Date has obeyed the DateFormat configuration. This is due to the Jackson limitation that I linked to above.

I see this behaviour both with and without Spring HATEOAS on the classpath. If you’re seeing behaviour that’s different to what I’ve described above, then please describe your application’s configuration and its dependencies in more detail. Ideally, please provide a sample that shows a DateTime always being serialised as a timestamp.

@csavory You make a good point about the Spring HATEOAS auto-configuration. Thanks. I’ve opened #2426 to improve the docs.