I have the below Code :
DTO :
Class MyDTO {
import java.util.Date;
private Date dateOfBirth;
public Date g
List itemCreate a class to extend JsonDeserializer
public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String date = jsonParser.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
Use @JsonDeserialize(using = CustomJsonDateDeserializer.class) annotation on setter methods.
Thanks @Varun Achar answer, url
@DateTimeFormat is for form backing (command) objects. Your JSON is processed (by default) by Jackson's ObjectMapper in Spring's MappingJackson2HttpMessageConverter (assuming the latest version of Jackson). This ObjectMapper has a number of default date formats it can handle. It seems yyyy-mm-dd is one of them, but dd-mm-yyyy is not.
You'll need to register your own date format with a ObjectMapper and register that ObjectMapper with the MappingJackson2HttpMessageConverter. Here are various ways to do that :
Alternatively, you can use a JsonDeserializer on either your whole class or one of its fields (the date). Examples in the link below