Andrew Tobilko Andrew Tobilko - 2 years ago 474
Java Question

LocalDateTime - deserialization with LocalDateTime.parse

I have the field

which serialises by
class to ISO-8601 format.

@JsonSerialize(using = ToStringSerializer.class)
private LocalDateTime initiationDate;

When I receive the following JSON,

"initiationDate": "2016-05-11T17:32:20.897",

I want to deserialize it by
LocalDateTime.parse(CharSequence text)
factory method. All my attempts ended with

Can not instantiate value of type [simple type, class
] from
value (
); no single-
constructor/factory method

How do I achieve that? How can I specify factory method?


The problem has been solved by including jackson-datatype-jsr310 module to the project and using

@JsonSerialize(using = ToStringSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime initiationDate;

Answer Source

Vanilla Jackson doesn't have a way to deserialize a LocalDateTime object from any JSON string value.

You have a few options. You can create and register your own JsonDeserializer which will use LocalDateTime#parse.

class ParseDeserializer extends StdDeserializer<LocalDateTime> {
    public ParseDeserializer() {

    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return LocalDateTime.parse(p.getValueAsString()); // or overloaded with an appropriate format
@JsonSerialize(using = ToStringSerializer.class)
@JsonDeserialize(using = ParseDeserializer.class)
private LocalDateTime initiationDate;

Or you can add Jackson's java.time extension to your classpath and register the appropriate Module with your ObjectMapper.

objectMapper.registerModule(new JavaTimeModule());

and let Jackson do the conversion for you. Internally, this uses LocalDateTime#parse with one of the standard formats. Fortunately, it supports values like


out of the box.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download