gpt4 book ai didi

java - "2017-09-11 14:28:42"的 Json 反序列化失败,显示 'while it seems to fit format ' yyyy-MM-dd'T'HH :mm:ss. SSS'Z '''

转载 作者:行者123 更新时间:2023-12-05 07:26:21 26 4
gpt4 key购买 nike

还有一些类似的线程,但我认为这是另一种情况。

我正在尝试从 JSON 反序列化日期字符串:

'2017-09-11 14:28:42' (without Z)

在我的 Json 文件中,该属性的类型为 java.util.Date 并注释为:

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

虽然我得到这个错误:

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))

为什么它会把我原来的字符串“2017-09-11 14:28:42”变成“2017-09-11 14:28:42Z”。为什么要添加这个“Z”?在其他线程中,解决方案是使用 @JsonFormat 或从字符串中删除此“Z”。好吧,我仍在使用 @JsonFormat 并且字符串中最初没有“Z”。

当通过 postman 请求资源时,我得到了相关属性的这个值:

    "deviceStatusSince": "2017-09-11 14:28:42",

如有任何帮助,我们将不胜感激。

编辑#1完整的堆栈跟踪:

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)

编辑#2:将属性设置为 null 时它起作用。但我想注意到创建和更新的属性的配置相似(@JsonAnnotation,相同的模式,相同的字符串)。那里有什么不同,我没看到什么?

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss", timezone = "Europe/Berlin") private Date created;

编辑#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}]

最佳答案

我通过复制创建的字段重新创建了另一个字段,并删除了这里提到的状态字段。新领域奏效了,然后我将其重命名。也许一个看不见的迹象是原因,我不知道。

关于java - "2017-09-11 14:28:42"的 Json 反序列化失败,显示 'while it seems to fit format ' yyyy-MM-dd'T'HH :mm:ss. SSS'Z ''',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54363280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com