Java – JsonDeserialize does not apply to LocalDateTime

JsonDeserialize does not apply to LocalDateTime… here is a solution to the problem.

JsonDeserialize does not apply to LocalDateTime


want to send a POST request from my client to my backend, and in the POJO I have two fields LocalDate and LocalDateTime, as shown below:

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy - hh:mm:ss")
private LocalDateTime createdTimestamp;

@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy")
private LocalDate expiredDate; 

The client will send the request body as follows:

    "expiredDate" : "01.01.2020",
    "createdTimestamp" : "01.02.2020 - 10:10:10"  

However, on the backend, I got an exception :

com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(Ljava/lang/Class; Ljava/lang/String; Ljava/lang/String; [Ljava/lang/Object;)Ljava/lang/Object;

If I leave createdTimestamp outside the body of the request, then it works. It seems that only the comment @JsonDeserialize (using = LocalDateDeserializer.class) is valid, while @JsonDeserialize (using = LocalDateTimeDeserializer.class) is not.

Does anyone know why this is so?

Related Problems and Solutions