spring|spring boot 传Date类型数据报错,无法读取JSON(无法从String构造java.util.Date实例)
前端传值:
{
"province":"省份",
"publish":"2021-10-01 00:00:00"
}
Entity:
@ApiModelProperty(value = "https://www.it610.com/article/推出时间")
private Date publish;
报错信息如下:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2021-10-01 00:00:00": not a valid representation (error: Failed to parse Date value '2021-10-01 00:00:00': Cannot parse date "2021-10-01 00: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 "2021-10-01 00:00:00": not a valid representation (error: Failed to parse Date value '2021-10-01 00:00:00': Cannot parse date "2021-10-01 00:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX',
parsing fails (leniency? null)) at [Source: (PushbackInputStream);
line: 3, column: 15] (through reference chain: houseHistory.entity.House["publish"])]
报错内容解释:
无法读取JSON:无法从String构造java.util.Date实例
value'2012-07-21 12:11:12':无效表示("yyyy-MM-dd'T'HH:mm:ss.SSSZ","yyyy-MM-dd'T'HH:mm:ss.SSS'Z'","EEE,dd MMM yyyy HH:mm :ss zzz","yyyy-MM-dd"))
【spring|spring boot 传Date类型数据报错,无法读取JSON(无法从String构造java.util.Date实例)】解决方法如下:
在实体Date类型的字段上使用@JsonFormat注解格式化日期,如下
@ApiModelProperty(value = "https://www.it610.com/article/推出时间")
@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
private Date publish;
推荐阅读
- 第326天
- 清明,是追思、是传承、是感恩。
- 午门传说
- 赵原野传|赵原野传 孤独
- Activiti(一)SpringBoot2集成Activiti6
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 员工的微信朋友圈是公司的宣传阵地吗()
- 从我的第一张健身卡谈传统健身房
- 种树郭橐驼传(文言句式+古今异义+词类活用+通假字)