My pojo class is as given below
public class Employee {
private int empId;
@NotEmpty
private String empName;
@DateTimeFormat(pattern = "dd/MM/yyyy")
private Date empDoj;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Date getEmpDoj() {
return empDoj;
}
public void setEmpDoj(Date empDoj) {
this.empDoj = empDoj;
}
I am accepting the request in controller as shown below
@RequestMapping(value = "/employee",method = RequestMethod.POST)
public Employee employee(@RequestBody @Valid Employee employee
) {
return employee ;
}
when I am sending the below JSON request from postman it is giving me an error
{
"empName":"Anubhav",
"empDoj":"10/10/2019"
}
JSON parse error: Cannot deserialize value of type java.util.Date
from String "10/10/2019": not a valid representation (error: Failed to parse Date value '10/10/2019': Cannot parse date "10/10/2019": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSX", "yyyy-MM-dd'T'HH:mm:ss.SSS", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.util.Date
from String "10/10/2019": not a valid representation (error: Failed to parse Date value '10/10/2019': Cannot parse date "10/10/2019": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSX", "yyyy-MM-dd'T'HH:mm:ss.SSS", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
at [Source: (PushbackInputStream); l
when I am doing the same for the method handler
@RequestMapping(value = "/dateformat",method = RequestMethod.POST)
public Date dateex(@DateTimeFormat(pattern = "dd-MM-yyyy") @RequestParam(value = "date")Date date) {
}
It is working fine. Converting string specified in given pattern to Date object then why not with Java class member. In the docs also it says it can be used either at method parameter or field formatted as given date type of field