-1

I am trying to parse JSON as local date, I tried multiple options including @jsonFormat but I am still getting the below exception - (I am using spring boot rest controller for accepting this request)

public class Student {
    private long rollNumber;
    private String firstName;
    private String lastName;
    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    private LocalDate dob;
    private Gender gender;
    private Grade grade;
}

json :

[{
"rollNumber":"1",
"firstName":"abc",
"lastName":"ere",
"dob":"08-06-1993",
"gender":"MALE",
"grade":"TENTH"

}]
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String \"08-06-1993\": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '08-06-1993' could not be parsed at index 0; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String \"08-06-1993\": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '08-06-1993' could not be parsed at index 0\n at [Source: (PushbackInputStream); line: 5, column: 7] (through reference chain: java.util.ArrayList[0]->com.example.application.restApp.model.Student[\"dob\"])\r\n\tat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:389)\r\n\tat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:342)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:185)\r\n\tat 
Mohammad Mirzaeyan
  • 845
  • 3
  • 11
  • 30
dileep
  • 21
  • 2

2 Answers2

1

JSON can not deserialize the time from string. Change date type and format as below.

@JsonFormat
  (shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
public Date dob;
S. Anushan
  • 728
  • 1
  • 6
  • 12
1

I figured out the issue, seems like when we don't have a no args constructor even having @jsonFormat does not seem to work. I was using Lombok and I missed adding a no args constructor , after adding that the issue resolved. Thanks everyone for the help.

Interestingly spring boot does not throw error stating no args constructor is not present and tries to do the default implementation.

dileep
  • 21
  • 2