I have a class with a field of type LocalDate
:
public class MyClass {
private LocalDate myDate;
}
I have to store the value as yyyy-MM-dd instead of as [yyyy, M, d]
Which can be done creating a LocalDateSerializer as indicated in https://stackoverflow.com/a/38731094/10850340 (DateTimeFormatter.ISO_LOCAL_DATE)
In my scenario, there are multiple sources from where the data is received. One of them sends the date as ISO_INSTANT '2011-12-03T10:15:30Z'. The LocalDateDeserializer:
public class LocalDateDeserializer extends StdDeserializer<LocalDate> {
private static final long serialVersionUID = 1L;
protected LocalDateDeserializer() {
super(LocalDate.class);
}
@Override
public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
return LocalDate.parse(jp.readValueAs(String.class));
}
}
throws the following WARN:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Text '2021-02-25T00:52:00.000Z' could not be parsed, unparsed text found at index 10; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Text '2021-02-25T00:52:00.000Z' could not be parsed, unparsed text found at index 10 (through reference chain: MyClass["myDate"])]
My understanding is that the received string is not parseable as LocalDate
: LocalDate.parse(jp.readValueAs(String.class));
What would be the best way to have LocalDateDeserializer accepting any valid date format but returning a LocalDate