Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot + Jackson + LocalDateTime: Date not parsed correctly

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?

like image 794
damian Avatar asked Oct 25 '17 16:10

damian


2 Answers

Just inspected my project. My deserializer:

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);
}

}
like image 78
dimirsen Z Avatar answered Nov 20 '22 21:11

dimirsen Z


I tried to reproduce your example and it worked well.

MyEntity

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

    public MyEntity(Integer id, LocalDateTime time) {
        this.id = id;
        this.time = time;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public LocalDateTime getTime() {
        return time;
    }

    public void setTime(LocalDateTime time) {
        this.time = time;
    }
}

LocalDateTimeApplication

@SpringBootApplication
public class LocalDateTimeApplication {

    public static void main(String[] args) {
        SpringApplication.run(LocalDateTimeApplication.class, args);
    }
}

SomeController

@RestController
@RequestMapping("/SomeController")
public class SomeController {

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<Object> getMyEntity() {
        MyEntity entity = new MyEntity(1, LocalDateTime.now());
        return new ResponseEntity<Object>(entity, HttpStatus.OK);
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.so.example</groupId>
    <artifactId>localDateTime</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>localDateTime</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

output

like image 42
Woland Avatar answered Nov 20 '22 20:11

Woland