Jackson date-format for OffsetDateTime in Spring Boot

We Are Going To Discuss About Jackson date-format for OffsetDateTime in Spring Boot. So lets Start this Java Article.

Jackson date-format for OffsetDateTime in Spring Boot

  1. Jackson date-format for OffsetDateTime in Spring Boot

    I ended up creating a new primary ObjectMapper bean, and registering a new module with a custom serializer for OffsetDateTime. I'm able to set my own date format in here, using java.time.format.DateTimeFormatter. I also had to register the JavaTimeModule with my mapper.

  2. Jackson date-format for OffsetDateTime in Spring Boot

    I ended up creating a new primary ObjectMapper bean, and registering a new module with a custom serializer for OffsetDateTime. I'm able to set my own date format in here, using java.time.format.DateTimeFormatter. I also had to register the JavaTimeModule with my mapper.

Solution 1

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

By doing that, you can get OffsetDateTime properties as ISO 8601 including offset in your target.

Original Author Utku A. Of This Content

Solution 2

So I’ve managed to figure out a solution, but if you have an alternative please post it.

I ended up creating a new primary ObjectMapper bean, and registering a new module with a custom serializer for OffsetDateTime. I’m able to set my own date format in here, using java.time.format.DateTimeFormatter. I also had to register the JavaTimeModule with my mapper.

@Configuration
public class JacksonOffsetDateTimeMapper{

    @Primary
    @Bean
    public ObjectMapper objectMapper() {

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(OffsetDateTime.class, new JsonSerializer<OffsetDateTime>() {
            @Override
            public void serialize(OffsetDateTime offsetDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
                jsonGenerator.writeString(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(offsetDateTime));
            }
        });
        objectMapper.registerModule(simpleModule);

        return objectMapper;
    }

}

Original Author Utku A. Of This Content

Solution 3

  1. Add jackson-datatype-jsr310 to your dependencies
  2. Add to application.properties:

    spring.jackson.serialization.write-dates-as-timestamps=false
    

You will get:

"lastUpdated": "2017-07-16T19:17:57.689Z"

Original Author Konstantin Pavlov Of This Content

Solution 4

Adding a dependency on jackson-modules-java8 worked for me (jackson-datatype-jsr310 is deprecated)

<!-- deserialize Java 8 date time types e.g OffsetDateTime --> 
<dependency> 
  <groupId>com.fasterxml.jackson.module</groupId>
  <artifactId>jackson-modules-java8</artifactId>
</dependency>

I also needed to add this for it to work:

 om.registerModule(new JavaTimeModule());

No need for the write-dates-as-timestamps=false or om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) – that is applicable for Java “Date” object.

I used this annotation:

 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")  

and get output like this:

“timestamp”:”2020-04-23T08:00:00.000-06:00″

Original Author LeslieM Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment