Wednesday, September 7, 2016

De/serializing LocalDateTime Using Jackson

java.time.LocalDateTime which represents a date-time as year-month-day-hour-minute-second, is introduced in jdk8.
When we deserialise the objects which contains LocalDateTime , using jackson, it will give comma separated values.


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
 
ObjectMapper mapper = new ObjectMapper();


JavaTimeModule javaTimeModule = new JavaTimeModule();

javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME));

mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

mapper.registerModule(javaTimeModule);


And also you need to add following dependency;

<dependency>
 <groupid>com.fasterxml.jackson.datatype</groupid>
 <artifactid>jackson-datatype-jsr310</artifactid>
 <version>2.6.5</version>
<dependency>

No comments:

Post a Comment