[英]Json Deserialization of “2017-09-11 14:28:42” fails with 'while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'''
there are still some similar threads but I think this is another situation. 仍然有一些类似的线索,但我认为这是另一种情况。
I am trying to deserialize a date-string from JSON: 我正在尝试从JSON反序列化日期字符串:
'2017-09-11 14:28:42' (without Z)
'2017-09-11 14:28:42'(无Z)
In my Json File the property is of type java.util.Date and is annotated with: 在我的Json文件中,该属性的类型为java.util.Date,并带有以下注释:
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss", timezone = "Europe/Berlin")
Although I am getting this error: 虽然我收到此错误:
com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2017-09-11 14:28:42": not a valid representation (error: Failed to parse Date value '2017-09-11 14:28:42': Can not parse date "2017-09-11 14:28:42Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null))
com.fasterxml.jackson.databind.exc.InvalidFormatException:无法从字符串“ 2017-09-11 14:28:42”反序列化类型为java.util.Date的值:不是有效的表示形式(错误:无法解析日期值'2017-09-11 14:28:42':无法解析日期“ 2017-09-11 14:28:42Z”:虽然看起来适合格式'yyyy-MM-dd'T'HH:mm:ss .SSS'Z'',解析失败(宽大?null))
Why the hell it is making "2017-09-11 14:28:42Z" of my original string "2017-09-11 14:28:42". 为什么要创建原始字符串“ 2017-09-11 14:28:42”的“ 2017-09-11 14:28:42Z”。 Why does it add this 'Z'?
为什么要加上这个“ Z”? In other threads the solution was to use @JsonFormat or to remove this 'Z' from the String.
在其他线程中,解决方案是使用@JsonFormat或从字符串中删除此“ Z”。 Well I am still using @JsonFormat and there originally is no 'Z' in the String.
好吧,我仍在使用@JsonFormat,并且字符串中最初没有'Z'。
When requesting the resource with postman I am getting this value for the concerned property: 当向邮递员请求资源时,我将获得有关属性的以下值:
"deviceStatusSince": "2017-09-11 14:28:42",
Any help would be grateful. 任何帮助将不胜感激。
Edit#1 Full StackTrace: 编辑#1 Full StackTrace:
E/ASYNCTASKLOADDEVICEFROM: Deserialization of response failed! response: [{"id":4,"deviceId":"abc123def","deviceName":"mymachinename","deviceType":"mymachinetype","deviceStatus":1,"deviceStatusSince":"2017-09-11 14:28:42","location":"somewhere","created":"2019-01-24 19:25:11","updated":"2019-01-24 19:25:11","lamport_timestamp":3},{"id":5,"deviceId":"abc123def","deviceName":"mymachinename","deviceType":"mymachinetype","deviceStatus":1,"deviceStatusSince":"2017-09-11 14:28:42","location":"somewhere","created":"2019-01-24 20:10:29","updated":"2019-01-24 20:10:29","lamport_timestamp":3}]
com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2017-09-11 14:28:42": not a valid representation (error: Failed to parse Date value '2017-09-11 14:28:42': Can not parse date "2017-09-11 14:28:42Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null))
at [Source: (String)"[{"id":4,"deviceId":"5123b85f316a9_01","deviceName":"Maschine #123","deviceType":"mymachinetype","deviceStatus":1,"deviceStatusSince":"2017-09-11 14:28:42","location":"somewhere","created":"2019-01-24 19:25:11","updated":"2019-01-24 19:25:11","lamport_timestamp":3},{"id":5,"deviceId":"abc123def","deviceName":"mymachinename","deviceType":"mymachinetype","deviceStatus":1,"deviceStatusSince":"2017-09-11 14:28:42","location":"somewhere","created":"2019-01-24 20:10:29","updated":"[truncated 45 chars]; line: 1, column: 135] (through reference chain: java.util.ArrayList[0]->my.project.models.json.DeviceJson["deviceStatusSince"])
at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1504)
at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:910)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:513)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:466)
at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:195)
at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:285)
at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:268)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:287)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:285)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:244)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3999)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2999)
at my.project.remote.async.AsyncTaskLoadDevicesFromServer.onPostExecute(AsyncTaskLoadDevicesFromServer.java:147)
at my.project.remote.async.AsyncTaskLoadDevicesFromServer.onPostExecute(AsyncTaskLoadDevicesFromServer.java:46)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.access$600(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/ASYNCTASKLOADDEVICEFROM: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Edit#2: When setting the property to null it works. 编辑#2:将属性设置为null时,它可以工作。 But I want to notice that properties created and updated are configured similarily (@JsonAnnotation, same pattern, same String).
但是我想注意到,创建和更新的属性配置类似(@JsonAnnotation,相同的模式,相同的String)。 Whats different there, what do I not see?
那里有什么不同,我看不到什么?
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss", timezone = "Europe/Berlin") private Date created;
Edit#3: This is the response used to be deserialized via objectMapper.readValue: 编辑#3:这是以前通过objectMapper.readValue反序列化的响应:
[{"id":8,"deviceId":"abc123def","deviceName":"mymachine","deviceType":"mymachinetype","deviceStatus":1,"deviceStatusSince":"2017-09-10 13:37:42","location":"somewhere","created":"2019-01-26 01:09:06","updated":"2019-01-26 01:09:06","lamport_timestamp":3}]
I recreated another field by copying from the created field and deleted the status field mentioned here. 我通过从创建的字段进行复制来重新创建了另一个字段,并删除了此处提到的状态字段。 The new field worked, i renamed it then.
新的领域起作用了,然后我将其重命名。 Maybe an invisible sign was the cause, I dont know.
我不知道可能是看不见的征兆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.