Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Jackson feature WRITE_DATES_AS_TIMESTAMPS not working in Spring Boot

I set spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false in the Spring Boot config but the Jackson serializer still produces [1942,4,2] instead of "1942-04-02" for a DateTime value.

Some debugging snapshots

  • In org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.Jackson2ObjectMapperBuilderCustomizerConfiguration.StandardJackson2ObjectMapperBuilderCustomizer#customize there's

    configureFeatures(builder, this.jacksonProperties.getSerialization());

    which shows that "WRITE_DATES_AS_TIMESTAMPS" -> "false"

  • Then a bit later in org.springframework.http.converter.json.Jackson2ObjectMapperBuilder#configure there's this loop

    for (Object feature : this.features.keySet()) { configureFeature(objectMapper, feature, this.features.get(feature)); }

    and again this.features says "WRITE_DATES_AS_TIMESTAMPS" -> "false"

  • Yet during serialzation of a DateTime com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase#useTimestamp says false because provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) returns false.

Attempts at fixing

  • Out of despair I replaced spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false with spring.jackson.serialization.write-dates-as-timestamps=false because I found that mentioned in a lot of places (even though the Boot documentation doesn't hint at this). What about this? They seem to be synonyms - no effect.
  • While writing this question SO suggested WRITE_DATES_AS_TIMESTAMPS not woking on Spring boot 1.3.5. The answer says to replace WebMvcConfigurationSupport with WebMvcConfigurerAdapter. While this does help indeed I fail to understand why so.
like image 613
Marcel Stör Avatar asked Aug 10 '17 09:08

Marcel Stör


People also ask

Does spring boot have Jackson ObjectMapper?

When using JSON format, Spring Boot will use an ObjectMapper instance to serialize responses and deserialize requests. In this article, we will take a look at the most common ways to configure the serialization and deserialization options.

Is Jackson included in spring boot?

Auto-configuration for Jackson is provided and Jackson is part of spring-boot-starter-json . When Jackson is on the classpath an ObjectMapper bean is automatically configured. Several configuration properties are provided for customizing the configuration of the ObjectMapper .

Should ObjectMapper be a bean?

Assuming no configuration is required, just a plain ObjectMapper will do. It's OK to be declared as static. But in environment like Spring, IMHO you should declare it as a bean. Spring will manage its lifecycle for you.

What is Spring Jackson default property inclusion?

spring.jackson.default-property-inclusion=always, non_null, non_absent, non_default, non_empty. Configuring the environment variables is the simplest approach.


1 Answers

Spring Boot takes the presence of a WebMvcConfigurationSupport bean as an indication that you want to take complete control of the configuration of Spring MVC. You'd typically end up with such a bean by using @EnableWebMvc but you could also declare your own bean or configuration class that is a WebMvcConfigurationSupport.

If you subclass WebMvcConfigurerAdapter rather than WebMvcConfigurationSupport you're making an additive change to Spring Boot's auto-configuration of Spring MVC rather than taking over completely.

Part of Spring Boot's auto-configuration of Spring MVC is to configure it to use the auto-configured ObjectMapper for HTTP message conversion. If you switch off Boot's auto-configuration of Spring MVC, it will use its own, separate ObjectMapper that is unaffected by any spring.jackson.* configuration settings.

like image 150
Andy Wilkinson Avatar answered Oct 10 '22 11:10

Andy Wilkinson