springboot+vue+elementui保存时间类型数据报错JSON parse error

1.目前环境条件:

①mysql数据库中存储的时间字段类型为:datetime

②:springboot中存储的类型为:LocalDateTime

③前端代码:

javascript 复制代码
<el-col :span="24">
        <el-form-item>
            <div slot="label">运维日期<font color="red">*</font></div>
            <el-date-picker
            v-model="form.itemTime"
            value-format="yyyy-MM-dd HH:mm:ss "
            class="filter-item"
            type="daterange"
            range-separator="至"
            start-placeholder="运维开始日期"
            end-placeholder="运维结束日期"
            :clearable="false"
            />
          </el-form-item>
      </el-col>

2.报错的问题:

java 复制代码
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2024-03-06": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2024-03-06' could not be parsed at index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2024-03-06": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2024-03-06' could not be parsed at index 10

后端报错的问题:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2024-03-06": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2024-03-06' could not be parsed at index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2024-03-06": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2024-03-06' could not be parsed at index 10

3.解决方法:

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField("om_modify_time")
private LocalDateTime modifyTime;

在实体类字段上加上:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

4.原因:

org.springframework.http.converter.HttpMessageNotReadableException 异常是Spring框架在尝试将HTTP请求体中的JSON数据转换为Java对象时抛出的异常。该异常指出在解析JSON时遇到了问题。

在这个具体的例子中,异常信息表明在尝试将字符串 "2024-03-06" 反序列化为 java.time.LocalDateTime 类型时失败了。java.time.LocalDateTime 需要一个完整的日期和时间(如 yyyy-MM-dd'T'HH:mm:ss),而 "2024-03-06" 只提供了一个日期,没有提供时间信息,因此无法被正确解析。

要解决这个问题,你有几个选项:

  1. 更改JSON数据:确保发送到服务器的JSON数据包含完整的日期和时间信息。

  2. 使用java.time.LocalDate代替java.time.LocalDateTime :如果你的字段只包含日期信息,那么应该使用 LocalDate 类型而不是 LocalDateTime

  3. 自定义日期格式 :如果你不能更改JSON数据,并且确实需要解析这种格式的日期,你可以使用 @JsonFormat 注解来指定自定义的日期格式。然而,请注意,这仅适用于 LocalDate,因为 LocalDateTime 需要时间信息。

  4. 使用@DateTimeFormat :如果你使用的是Spring MVC的 @RequestParam@ModelAttribute,你可以使用 @DateTimeFormat 注解来指定日期格式。

  5. 全局配置:在Jackson的配置中设置全局的日期格式。

相关推荐
2401_857439691 小时前
Spring Boot新闻推荐系统:用户体验优化
spring boot·后端·ux
进击的女IT2 小时前
SpringBoot上传图片实现本地存储以及实现直接上传阿里云OSS
java·spring boot·后端
杨半仙儿还未成仙儿3 小时前
Spring框架:Spring Core、Spring AOP、Spring MVC、Spring Boot、Spring Cloud等组件的基本原理及使用
spring boot·spring·mvc
一 乐3 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
小御姐@stella3 小时前
Vue 之组件插槽Slot用法(组件间通信一种方式)
前端·javascript·vue.js
IT学长编程5 小时前
计算机毕业设计 二手图书交易系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·二手图书交易系统
艾伦~耶格尔6 小时前
Spring Boot 三层架构开发模式入门
java·spring boot·后端·架构·三层架构
man20176 小时前
基于spring boot的篮球论坛系统
java·spring boot·后端
万叶学编程6 小时前
Day02-JavaScript-Vue
前端·javascript·vue.js
Java探秘者7 小时前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea