I have a Spring MVC REST backend application. I'm sending the date in "mm/dd/yyyy" format to the client.
But if I format the date in "mm/dd/yyyy" and POST it back, data binding fails.
Here is my Spring customdateserializer obj that converts java.util.date to mm/dd/yyy when sending the date to the frontend
public class CustomDateSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2) throws
IOException, JsonProcessingException {
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = formatter.format(value);
gen.writeString(formattedDate);
}
}
Here is the object for which data binding fails
@Entity
@Table(name = "bill")
public class Bill implements GenericObject {
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date billDate;
@JsonSerialize(using = CustomDateSerializer.class)
public Date getBillDate() {
return billDate;
}
public void setBillDate(Date billDate) {
this.billDate = billDate;
}
}
Here is the end point to which I'm posting the data (Spring MVC controller)
@RequestMapping(value = { "/user/{userId}/bill" }, method = { RequestMethod.POST })
public void addBill(@RequestBody Bill bill_p,@PathVariable("userId") int userId,
HttpServletResponse httpResponse_p, WebRequest request_p) {
processing......
}