我有一个 Spring MVC REST 后端应用程序。我以“mm/dd/yyyy”格式向客户发送日期。
但是,如果我将日期格式化为“mm/dd/yyyy”并将其发布回来,数据绑定将失败。
这是我的 Spring customdateserializer obj,它在将日期发送到前端时将 java.util.date 转换为 mm/dd/yyy
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);
}
}
这是数据绑定失败的对象
@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;
}
}
这是我发布数据的终点(Spring MVC 控制器)
@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......
}