Cannot deserialize value of type `java.util.Date` from String,expected format “yyyy-MM-dd HH:mm:ss“

在使用elementplus的 el-date-picker 的时候后端报错:

 Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2022-09-08T00:00:00.000Z": expected format "yyyy-MM-dd HH:mm:ss"; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2022-09-08T00:00:00.000Z": expected format "yyyy-MM-dd HH:mm:ss"<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 76] (through reference chain: com.aifast.domain.Application["startTime"])]

<el-date-picker
    v-model="editApplication.endTime"
    type="datetime"
    format="YYYY-MM-DD HH:mm:ss"
    placeholder="请选择结束时间"
/>

后端加上注解也不管用

 @TableField("start_time")
 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss",locale="zh")
 private Date startTime;

打印前端往后端传的值,如下图:

 发现并没有格式化。

后来再翻阅elementplus el-date-picker 的属性发现了 value-format 属性,官方的解释如下

属性名 说明 类型 可选值 默认值
value-format 可选,绑定值的格式。 不指定则绑定值为 Date 对象 string 查看 日期格式 ——

后来指定了value-format就搞定了。

<el-date-picker
    v-model="editApplication.startTime"
    type="datetime"
    format="YYYY-MM-DD HH:mm:ss"
    value-format="YYYY-MM-DD HH:mm:ss"
    placeholder="请选择开始时间"
/>

猜你喜欢

转载自blog.csdn.net/Yajyaj123/article/details/126959172