Could not read document: Cannot deserialize value of type `java.util.Date` from String

本文详细介绍了在使用Java处理日期时遇到的常见错误:当从前端接收到不符合预期格式的日期字符串时,后端出现的HttpMessageNotReadableException。文章提供了具体的错误信息,并给出了解决方案,即建议前端在传递日期参数时,使用new Date('timeStr')将字符串转换为日期对象。

文章目录

现象

    @ResponseBody
    @RequestMapping("/ooxx/save")
    public ResultStatus saveOOxx(@RequestBody ooxx ooxx, HttpSession session, Model model) {}

后台接受实体类时,报错

[WARN](2020-04-20 16:43:59,046)-Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Cannot deserialize value of type `java.util.Date` from String "2020-04-20 00:00:00": not a valid representation (error: Failed to parse Date value '2020-04-20 00:00:00': Cannot parse date "2020-04-20 00:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))
 at [Source: (PushbackInputStream); line: 1, column: 127] (through reference chain: com.ooxx.pojo.ooxx["ooxx"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2020-04-20 00:00:00": not a valid representation (error: Failed to parse Date value '2020-04-20 00:00:00': Cannot parse date "2020-04-20 00:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))
 at [Source: (PushbackInputStream); line: 1, column: 127] (through reference chain: com.ooxx.pojo.ooxx["ooxx"])-org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotReadable(DefaultHandlerExceptionResolver.java:377)-

解决

前端传时间属性的参数时,不要直接传字符串,使用 new Date(‘timeStr’) 把这个字符串包起来就好了

var request = {
//ooxx: finishDate
ooxx: new Date(finishDate)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值