Spring Mvc使用Jackson进行json转对象时,遇到的字符串转日期的异常处理(JSON parse error: Can not deserialize value of type java.util.Date from String)

时间:2018-10-16 01:00:19   收藏:0   阅读:926

1.问题排查

- 项目配置

- 出现的场景:

feign.codec.DecodeException: JSON parse error: Can not deserialize value of type java.util.Date from String "2018-03-07 16:18:35": not a valid representation (error: Failed to parse Date value ‘2018-03-07 16:18:35‘: Can not parse date "2018-03-07 16:18:35Z": while it seems to fit format ‘yyyy-MM-dd‘T‘HH:mm:ss.SSS‘Z‘‘, parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2018-03-07 16:18:35": not a valid representation (error: Failed to parse Date value ‘2018-03-07 16:18:35‘: Can not parse date "2018-03-07 16:18:35Z": while it seems to fit format ‘yyyy-MM-dd‘T‘HH:mm:ss.SSS‘Z‘‘, parsing fails (leniency? null))

2.解决方法

因为数据库中存在一个create_time字段,属性是timestamp,服务器端查询数据库并把字段映射到定义的Vo中出现了格式问题,所以在Vo中把createTime添加@JsonFormat注释就可以完成时间字段映射的格式问题。

@JsonFormat( pattern="yyyy-MM-dd HH:mm:ss")  
  private Date createTime;  

3.报异常的原因

参考:https://blog.csdn.net/qq906627950/article/details/79503801
这篇博客可以了解底层报错的原因。

原文:https://www.cnblogs.com/liaoguanwang/p/9795195.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!