3

I would like to send POST request from my client to my backend, and in the POJO I have two fields LocalDate and LocalDateTime as follows:

@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 request with body like:

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

In the backend, however, I got an exception:

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

And if I leave the createdTimestamp out of request's body then it worked. It seems that only the annotation @JsonDeserialize(using = LocalDateDeserializer.class) worked, while the @JsonDeserialize(using = LocalDateTimeDeserializer.class) did not work.

Does anyone have an idea why this happened?

Ock
  • 418
  • 8
  • 24
  • 3
    You have a lowercase `h` for hours in your pattern, but there's no `a` specifying if those hours are `AM` or `PM`. That might be a problem, but I am not sure. Make the hours be uppercase `H` to describe hours in 24h-format and try again. Could work... – deHaar Aug 27 '20 at 10:53
  • 1
    yes as said @deHaar your POJO properties does not match the one of your client : `createdTimestamp != createTimestamp` or is that a typo ? – Logan Wlv Aug 27 '20 at 11:11
  • Sorry the createdTimestamp is my typo, the solution from deHaar worked for me. Thank you very much! – Ock Aug 27 '20 at 11:52
  • Good... I made it an answer for future readers. – deHaar Aug 27 '20 at 12:17

1 Answers1

3

You are using the symbol for hours in 12h format (hh) in your pattern, but the pattern is incomplete because it is missing an indicator for AM/PM, which would be an a. Leaving that indicator makes the time expression ambiguous, it could be 10 AM or 10 PM (22 in 24h-format).

You could just switch the format to 24h format (HH), which would make the time unambiguous.

Like this:

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy - HH:mm:ss")
private LocalDateTime createdTimestamp;
deHaar
  • 17,687
  • 10
  • 38
  • 51