Spring Boot Jackson date format – Jackson

by
Ali Hasan
cucumber-java date-format jackson spring-boot

The Problem:

A Spring Boot application is encountering an issue with Jackson’s date formatting. Despite configuring the application to return dates in the format ‘yyyy-MM-dd’T’HH:mm:ss.SSS’Z’, the response from the controller shows dates in the format ‘yyyy-MM-dd’T’HH:mm:ss’Z’. The problem persists despite trying various solutions, including annotations, configurations, and manually creating an ObjectMapper. The application uses Java 17, SpringBoot 2.7.0, and Jackson 2.13.3.

The Solutions:

Solution 1: Custom JSON Serializer

You can create a custom JSON serializer for the Instant type:

public static class InstantSerializer extends StdSerializer<Instant> {

    public InstantSerializer() {
        super(Instant.class);
    }

    @Override
    public void serialize(
        @Nullable final Instant value,
        @NonNull final JsonGenerator jsonGenerator,
        @NonNull final SerializerProvider serializerProvider
    ) throws IOException {
        if (value != null) {
            final DateTimeFormatter formatter = DateTimeFormatter
                .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                .withZone(ZoneId.systemDefault());
            final String text = formatter.format(value);
            jsonGenerator.writeString(text);
        }
    }

}

Then, annotate the fields in the SlotResponse class with the custom serializer:

    @Data
    public class SlotResponse {

        @JsonSerialize(using = InstantSerializer.class)
        private Instant start;
        @JsonSerialize(using = InstantSerializer.class)
        private Instant end;
    }

This will serialize the Instant fields in the desired format.

Alternatively, if you want to apply this serializer globally, you can add it to the ObjectMapper:

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        ...
        final SimpleModule module = new SimpleModule();
        module.addSerializer(new InstantSerializer());
        mapper.registerModule(module);
        ...
        return mapper;
    }

Solution 2: Jackson.TimeZone Customization

To resolve the issue, it is necessary to configure the Jackson’s time zone setting through either of the following methods:

  1. @JsonFormat(timezone = "..."): Annotate the Instant field with @JsonFormat and specify the desired time zone. For example: @JsonFormat(timezone = "UTC").

  2. spring.jackson.time-zone: Set the spring.jackson.time-zone property in the application configuration to the desired time zone. For example: spring.jackson.time-zone=UTC.

Additionally, ensure that the spring.jackson.serialization.WRITE_DATES_WITH_CONTEXT_TIME_ZONE property is set to true to enable the use of the configured time zone for date serialization.

By implementing these settings, Jackson will serialize the Instant values with the specified time zone, resulting in the desired format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'.

Q&A

How to change the date format in SpringBoot with Jackson

Create your own serializer and replace the existing one in jackson or use spring.jackson.date-format=yyyyMMddTHH:mm:ss.SSSZ

I am getting the time in format yyyy-MM-dd’T’HH:mm:ss’Z’ but want yyyy-MM-dd’T’HH:mm:ss.SSS’Z’

Add @JsonFormat(pattern=&quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS&#39;Z&#39;&quot;) to the field or create your own custom serializer.

Video Explanation:

The following video, titled "Jackson date-format for OffsetDateTime in Spring Boot - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

java: Jackson date-format for OffsetDateTime in Spring Boot Thanks for taking the time to learn more. In this video I'll go through your ...