对于使用element-ui中的日期时间选择器产生的json数据转换格式报错

发布时间 2023-03-29 16:27:51作者: hzc8893

对于使用element-ui中的日期时间选择器产生的json数据转换格式报错

报错如下所示

JSON parse error: Cannot deserialize value of type java.time.LocalDateTime from String "2023-03-29 19:00:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2023-03-29 19:00:00' could not be parsed, unparsed text found at index 16; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.time.LocalDateTime from String "2023-03-29 19:00:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2023-03-29 19:00:00' could not be parsed, unparsed text found at index 16 at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 120]

前端

在日期选择器标签中添加日期格式化 value-format="yyyy-MM-dd HH:mm:ss"

<el-form-item label="时间" prop="meetingStartTime">
     <el-date-picker v-model="time" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss">
     </el-date-picker>
</el-form-item>

后端

在接受参数的Dto类中在对应的日期类中的成员变量上, 添加@JsonFormat注解

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
private LocalDateTime meetingEndTime;

然后问题就解决了