springboot 提交时间字符串报错 Failed to convert property value of type ‘java.lang.String‘ to required ‘Date‘

Posted yqj234

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot 提交时间字符串报错 Failed to convert property value of type ‘java.lang.String‘ to required ‘Date‘相关的知识,希望对你有一定的参考价值。

Field error in object 'accountPeriod' on field 'endDate': rejected value [2021-12-31]; codes [typeMismatch.accountPeriod.endDate,typeMismatch.endDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [accountPeriod.endDate,endDate]; arguments []; default message [endDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.databind.annotation.JsonSerialize @javax.persistence.Temporal @javax.persistence.Column java.util.Date] for value '2021-12-31'; nested exception is java.lang.IllegalArgumentException]
Field error in object 'accountPeriod' on field 'startDate': rejected value [2021-12-11]; codes [typeMismatch.accountPeriod.startDate,typeMismatch.startDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [accountPeriod.startDate,startDate]; arguments []; default message [startDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'startDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.databind.annotation.JsonSerialize @javax.persistence.Temporal @javax.persistence.Column java.util.Date] for value '2021-12-11'; nested exception is java.lang.IllegalArgumentException]

  

解决方法:


第一方案:在实体类的属性上加上 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 注解。

如:  
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "LOGIN_TIME")
private Date loginTime;

刚才发现,如果转换的时间格式定义为 "yyyy-MM-dd HH:mm:ss",

那页面传到后台的字符串是这种格式的:"2019-01-31 14:33",后台还是会报错,因为字符串里没有 秒 的数据。


第二个方案: 是在 controller 里加一个方法:

    @InitBinder
    public void initBinder(WebDataBinder binder)
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(true);
        // true:允许输入空值,false:不能为空值
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    


如下图所示:

以上是关于springboot 提交时间字符串报错 Failed to convert property value of type ‘java.lang.String‘ to required ‘Date‘的主要内容,如果未能解决你的问题,请参考以下文章

springboot 提交时间字符串报错 Failed to convert property value of type ‘java.lang.String‘ to required ‘Date‘

springboot 提交时间字符串报错 Failed to convert property value of type ‘java.lang.String‘ to required ‘Date‘

springboot 提交时间字符串报错 Failed to convert property value of type ‘java.lang.String‘ to required ‘Date‘

Ajax提交数据SpringBoot后台报错“HttpMessageNotReadableException: JSON parse error: Cannot construct instance“

springboot maven项目运行常见报错 及ajax请求报错

springboot项目传参报错