我从客户端传递给api对象
@ApiModel(value = "EMail", description = "Model with property for sending email")
@Document(collection = "email")
public class EMail extends Letter {
@ApiModelProperty(value = "Date send email")
private Date sendEmail;
public Date getSendEmail() {
return sendEmail;
}
public void setSendEmail(Date sendEmail) {
this.sendEmail = sendEmail;
}
这是我的控制器
@ApiOperation(value = "Save email")
@CrossOrigin
@RequestMapping(value = "/saveEmail", method = RequestMethod.POST)
public void saveEmail(EMail eMail){
eMailService.saveEmail(eMail);
}
当我将Email传递给api时,我收到错误消息,尽管启动日期采用ISO Date_Time格式(Spring Boot的默认设置),但Spring Boot无法将String转换为Date。 这是例外
Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiModelProperty @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2018-08-03T07:38:17.856+0000'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-08-03T07:38:17.856+0000]","objectName":"EMail","field":"sendEmail","rejectedValue":"2018-08-03T07:38:17.856+0000","bindingFailure":true,"code":"typeMismatch"}],"message":"Validation failed for object='EMail'. Error count: 1","path":"/api/saveEmail"}
答案 0 :(得分:1)
您将需要使用@org.springframework.format.annotation.DateTimeFormat
@ApiModel(value = "EMail", description = "Model with property for sending email")
@Document(collection = "email")
public class EMail extends Letter {
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
@ApiModelProperty(value = "Date send email")
private Date sendEmail;
public Date getSendEmail() {
return sendEmail;
}
public void setSendEmail(Date sendEmail) {
this.sendEmail = sendEmail;
}
}
这将对查询和路径参数应用正确的绑定。