开发过程中碰到的问题:
前端一个修改请求,传的请求体中有时间参数,前后端都写好了格式为yyyy-MM-dd HH:mm:ss,但是后端Jackson解析仍然报错。
实际错误演示:
这是前端时间展示方式,在修改数据时,封装的请求参数中createTime属性值为2024-08-02 15:00:00。但是到了后端接收时,却提示Cannot parse date "2024-08-02 15:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX'。
以下是完整报错:
JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2024-08-02 15:00:00": not a valid representation (error: Failed to parse Date value '2024-08-02 15:00:00': Cannot parse date "2024-08-02 15:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2024-08-02 15:00:00": not a valid representation (error: Failed to parse Date value '2024-08-02 15:00:00': Cannot parse date "2024-08-02 15:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null)) at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 31] (through reference chain: com.ruoyi.basetable.domain.RdeTable["createTime"])
而后端已经加了:
/** 创建时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
private Date createTime;
仍然会提示解析失败,解决办法如下,亲测有效,在application.yml中添加如下配置:
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss