My spring boot 1.3.5 application is using jackson with the dependency "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.5.0".
Encountering an issue when a user inputs a date value in a format different than expected (mm/dd/yyyy) during a POST request, resulting in a severe error displayed on the UI without any logs in the server:
"Could not read document: Text '08-21-1999' could not be parsed at index 2 (through reference chain: com.xxx.yyy.MyDTO["firstofficevisit"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Text '08-21-1999' could not be parsed at index 2 (through reference chain: com.xxx.yyy.MyDTO["firstofficevisit"])"
The JSON payload causing this issue shows that dateOfBirth works fine but firstofficevisit triggers the aforementioned error:
{
"patientId":3,
"dateOfBirth":"04/11/1984",
"firstofficevisit":"08-21-1999",
...
}
The DTO class includes the following annotations for date formatting:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy")
private LocalDate dateOfBirth;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy")
private LocalDate firstofficevisit;
However, dates need to be submitted exactly in the specified format for them to be processed correctly. I have a utility called DateUtil in Java which handles date conversions, and I am considering integrating it with my DTO:
public class DateUtil {
...
The challenge lies in validating the various date formats as strings coming from the client side, especially with multiple date fields involved. I'm looking for suggestions on whether Jackson can be configured to work more flexibly with DateUtil.java or if enforcing validation on the client side would be a better approach. Any examples or insights shared would be greatly appreciated.