添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Java – Spring boot + Jackson + LocalDateTime: Date not parsed correctly

jackson java java-8 spring-boot

I have a LocalDateTime property in my entity class, but when it's serialized, I don't see the expected format.

This is the class:

public class MyEntity {
     private Integer id;
     @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
     private LocalDateTime changeDate;
     // getters and setters

This is how jackson is formatting it:

"id": 56, "changeDate": { "hour":14, "minute":19, "nano":797000000, "second":7, "dayOfMonth":24, "dayOfWeek":"TUESDAY", "dayOfYear":297, "month":"OCTOBER", "monthValue":10, "year":2017, "chronology": { "id":"ISO", "calendarType":"iso8601"

Note that I added the following dependency to my pom:

<dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>

I don't include the version because spring boot takes care of that.
By the way I'm using spring boot 1.5.2.RELEASE.

I also included the following property in application.properties:

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

Any idea why the date is not formatted with like that instead of using the pattern I provided?

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.time.LocalDateTime;
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;
public class JsonDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
private static final String NULL_VALUE = "null";
@Override
public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {
    ObjectCodec oc = jp.getCodec();
    JsonNode node = oc.readTree(jp);
    String dateString = node.textValue();
    LocalDateTime dateTime = null;
    if (!NULL_VALUE.equals(dateString)) {
        dateTime = LocalDateTime.parse(dateString, ISO_LOCAL_DATE_TIME);
    return dateTime;

My serializer:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.time.LocalDateTime;
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;
public class JsonLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime dateTime, JsonGenerator generator, SerializerProvider provider)
        throws IOException {
    String dateTimeString = dateTime.format(ISO_LOCAL_DATE_TIME);
    generator.writeString(dateTimeString);

You need to set your own formatter.

In my RestConfig I have:

@Configuration
@ComponentScan(value = {"ru.outofrange.controller"})
public class RestConfig extends RepositoryRestConfigurerAdapter {
@Override
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    registerSerializerDeserializer(objectMapper);
private void registerSerializerDeserializer(ObjectMapper objectMapper) {
    SimpleModule module = new SimpleModule();
    module.addSerializer(LocalDateTime.class, new JsonLocalDateTimeSerializer());
    module.addDeserializer(LocalDateTime.class, new JsonDateTimeDeserializer());
    objectMapper.registerModule(module);