0

i am not able to get ISO formatted dates.

Setup:

  • Kotlin 1.7.10
  • Spring Boot 2.7.1
  • Jackson 2.13.3

with following jackson dependencies:

  • jackson-module-kotlin
  • jackson-databind

Application properties

    spring:   
      jackson:
        date-format: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
        default-property-inclusion: non_null
        serialization:
          write-dates-as-timestamps: false
          INDENT_OUTPUT: true
        deserialization:
          FAIL_ON_UNKNOWN_PROPERTIES: false
          FAIL_ON_INVALID_SUBTYPE: false

I also tried moving this config to class level but no difference.

It only works when i use property annotations like:

  @JsonProperty("startTimestamp")
  @JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
  var startTimestamp: LocalDateTime

But that's not an option.

Edit

I also tried:

  • Overriding the default configuration of ObjectMapper
  • Customizing the ObjectMapper using Jackson2ObjectMapperBuilderCustomizer
Papepula
  • 11
  • 3
  • Does this answer your question? [How to Use Spring Boot's 'spring.jackson.date-format' Property?](https://stackoverflow.com/questions/43622259/how-to-use-spring-boots-spring-jackson-date-format-property) – somethingsomething Jul 18 '22 at 15:30
  • Unfortunately not, java.util.Date and adding a custom serializer didn't do anything – Papepula Jul 19 '22 at 05:49

1 Answers1

0

@Configuration public class JacksonConfig {

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper objectMapper = JsonMapper.builder()
            .findAndAddModules()
            .defaultDateFormat(new StdDateFormat().withColonInTimeZone(true))
            .build();

    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    objectMapper.setTimeZone(TimeZone.getTimeZone("UTC"));
    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));

    return objectMapper;
}

}