在使用Postman测试Spring Boot项目接口时,接口返回JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String错误,如下图:

JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String 随笔 第1张

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 

参数使用JSON格式,POST请求,如下图:

JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String 随笔 第2张

 

createTime变量使用了字符串类型,接口接收参数后将该字符串转为日期时,发生错误,项目中使用fastjson来处理json数据。

此问题解决方法

在接收参数实体类的createtime字段添加JsonFormat注解

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")

如下图:

JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String 随笔 第3张

 

项目重启,再次请求,如果如下:

JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String 随笔 第4张

 

请求成功,如果使用了Timestamp类型来接口,有可能会报下面的异常:

JSON parse error: Can not deserialize value of type java.sql.Timestamp from String

这时,使用上面的方法也可解决问题。

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄