我得到下面的異常,當我嘗試閱讀的字符串消息的到來。獲取無法識別的屬性例外,雖然物業已經提到
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "EventID" (class org.json.JSONObject), not marked as ignorable (0 known properties: ])
at [Source: {"MessageType": "EventSubscription","MessageData": {"EventID": ["ProximitySensorInRange", "ProximitySensorOutOfRange", "BarcodeBarcodeScanned", "RFIDRFIDScanned", "EXTERNALRFIDEXTERNALRFIDScanned", "ButtonsButtonPressed", "TestClientEvent", "ApplicationInteractionDetected", "ProductAdditionMethodBarcode", "ProductAdditionMethodRFID", "ProductAdditionMethodSearchAndAdd", "KohlsOfferKohlsCashUsed", "KohlsOfferGiftCardUsed", "KohlsOfferPromoCodesUsed", "SearchInputKeyedIn", "SearchInputVoice", "KubeUsageTime", "ProductAdditionMethodRecommendation", "ProductAdditionMethodUPCSearch"]}}; line: 1, column: 65] (through reference chain: com.kube.dataobjects.HostEventObject["MessageData"]->org.json.JSONObject["EventID"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:731)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:915)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1292)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1270)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:247)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:538)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:106)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2146)
at com.kube.websocketserver.KubeWebSocketServer.onMessage(KubeWebSocketServer.java:80)
at org.java_websocket.server.WebSocketServer.onWebsocketMessage(WebSocketServer.java:469)
at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:368)
at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:157)
at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:657)
這是IM如何閱讀郵件。
hostEvent = mapper.readValue(message,HostEventObject.class);
這是我的HostEventObject類。
下面給出的是我的JSON字符串消息。
{
MessageType: "EventSubscription",
MessageData: {
EventID: ["test1","test2"]
}
}
爲什麼我會得到這個異常?我已閱讀過類似的問題,但我沒有犯拼寫錯誤,也不能忽略此屬性。請指教。
不會要花Messagedata作爲一個JSON對象並讀取事件ID值?當我使用Newtonsoft在C#中編寫它時,我將它聲明爲一個JObject,並且沒有收到任何錯誤。請指教。 – mayooran
它看起來不像。 Groovy JSON反序列化可以給你更多的動態行爲,類似於你所期待的。 Jackson可能還有一個支持它的模塊,但是從錯誤消息中不支持。 –
參考JSON.org模塊爲Jackson提供了更新的答案,它應該會給你你所追求的動態行爲。將消息數據發佈爲地圖<字符串列表>也可能會起作用。 –