Post时间戳参数作为Play的日期!框架模型



我想玩!框架将通过POST发送的时间戳转换为模型中的java.util.Date格式,但我不知道它是否直接可能。

这是我的模型:

public class Contact extends Model {
    @Id
    private Long id;
    @Constraints.Required
    private String name;
    @JsonIgnore
    @Temporal(TemporalType.TIMESTAMP)
    private Date removed = null; // When the contact is no longer active
}

我试图将@Formats.DateTime(pattern="?")添加到删除,但由于DateTime使用SimpleDateFormat,我无法找到用于将时间戳转换为正确日期的模式。

好吧,我来回答我自己的问题,这是我所做的(也许不是最好的方法,但它有效)。

我不使用模型将张贴的参数与删除的值相匹配,而是在我的控制器中这样做:

String[] accepts = {"name", "datestamp"};
Form<Contact> form = Form.form(Contact.class).bindFromRequest(accepts);
Date date = null;
try {
    date = new Date(Long.parseLong(form.field("datestamp").value()));
}
catch (NumberFormatException nfe) {}
if (date == null) {
    form.reject("date", "error.invalid");
}
if (form.hasErrors()) {
    return badRequest(form.errorsAsJson());
}
else {
    Contact contact = form.get();
    contact.setRemoved(date);
    contact.save();
    return ok();
}

最新更新