Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.fasterxml.jackson.databind.exc.InvalidDefinitionException:Cannot construct instance of`java.time.Instant`(no Creators,like default construct):

Tags:

json

jackson

I already went through link: JSON parse error: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value, but getting below error.

Error:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.Instant` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-08-28T15:15:44.183Z')

Java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class StudentResponse {
    private Integer a;
    private String b;
    private String c;
    private String d;
    private String e;
    private Instant lastUpdateDate;
}
like image 858
PAA Avatar asked Aug 28 '19 12:08

PAA


1 Answers

I was able to parse a JSON where Instant field is "2019-08-28T15:15:44.183Z"

First of all, ensure you have a dependency. E.g. in build.gradle

implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'

Then configure ObjectMapper to use this dependency and not (de)serialize Instant as timestamps that it does by default. I setup my ObjectMapper as:

ObjectMapper mapper  = new ObjectMapper()
        .findAndRegisterModules()
        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
like image 163
Manos Nikolaidis Avatar answered Nov 15 '22 06:11

Manos Nikolaidis