我有这样的字符串:
{ "debug":"false","switchTime":"2017-04-12 17:04:42.896026" }
我试图通过这种方法获得对象:
new ObjectMapper().readValue(string,MyObject.class);
和MyObject类:
class MyObject { private Boolean debug; private Timestamp switchTime; //...getters,setters,constructors }
我有这样的例外:
com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.sql.Timestamp from String "2017-04-12 17:04:42.896026": not a valid representation (error: Failed to parse Date value '2017-04-12 17:04:42.896026': Can not parse date "2017-04-12 17:04:42.896026Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'',parsing fails (leniency? null)) at [Source: {"debug":"false","switchTime":"2017-04-12 17:04:42.896026"};
我不明白为什么……如果我在调试模式下使用Timestamp.valueOf()和“2017-04-12 17:04:42.896026” – 我有成功
解决方法
我想你需要使用@JsonFormat注释设置预期的日期/时间格式,如下所示.
class MyObject { private Boolean debug; @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS") private Timestamp switchTime; //...getters,constructors }
您还可以将时区设置为@JsonFormat(pattern =“yyyy-MM-dd HH:mm:ss.SSS”,timezone =“PST”)