当我尝试读取来自的字符串消息时,我得到以下异常。
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)
这是我正在阅读的消息。
hostEvent = mapper.readValue(message,HostEventObject.class);
这是我的HostEventObject类。
public class HostEventObject {
public static final String MESSAGETYPE_SUBSCRIPTION = "eventsubscription";
public static final String MESSAGETYPE_EVENTFIRED = "eventfired";
public static final String MESSAGETYPE_FIREEVENT = "fireevent";
@JsonProperty("APIVersion")
private String APIVersion;
@JsonProperty("EventTime")
private String EventTime;
@JsonProperty("MessageType")
public String MessageType;
@JsonProperty("MessageData")
public JSONObject MessageData;
}
下面给出的是我的JSON字符串消息。
{
MessageType: "EventSubscription",
MessageData: {
EventID: ["test1","test2"]
}
}
为什么我会收到此异常?我已经阅读了类似的问题,但我没有犯拼写错误,也不能忽略这个属性。请指教。
答案 0 :(得分:1)
MessageData的类型为JSONObject,它没有名为EventID的字段。可能你打算在这里放一个不同的课程?
这是你的JSON
MessageData: {
EventID: ["test1","test2"]
}
这要求Message Data的数据类型有一个EventID字段,它是某种List / Set / Array或其他字符串集合。但我们拥有的是 -
@JsonProperty("MessageData")
public JSONObject MessageData;
更新看起来有一个用于JSON组织的Jackson模块:请参阅https://github.com/FasterXML/jackson-datatype-json-org。要注册模块,您可以使用: -
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JsonOrgModule());
答案 1 :(得分:0)
您的POJO似乎缺少属性EventID。 你可以通过2种方式解决它
在java POJO中添加属性。
将代码放在忽略上面的属性上,可以通过两种方式实现:
一个。标记POJO以忽略未知属性
@JsonIgnoreProperties(ignoreUnknown = true)
湾配置用于序列化/反序列化POJO / json的ObjectMapper,如下所示:
ObjectMapper mapper =new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);