gpt4 book ai didi

java - 尽管已提及该属性,但出现无法识别的属性异常

转载 作者:行者123 更新时间:2023-11-30 07:55:48 25 4
gpt4 key购买 nike

当我尝试读取传入的字符串消息时,出现以下异常。

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"]
}
}

为什么我会收到此异常?我读过类似的问题,但我没有犯拼写错误,也不能忽略这个属性。请指教。

最佳答案

MessageData 的类型为 JSONObject它没有名为 EventID 的字段。也许您打算在这里放置一个不同的类?

这是您的 JSON

 MessageData: {
EventID: ["test1","test2"]
}

这要求消息数据的数据类型具有 EventID 字段,该字段是某种列表/集合/数组或其他字符串集合。但我们拥有的是 -

 @JsonProperty("MessageData")
public JSONObject MessageData;

更新 看起来有一个 JSON 组织的 Jackson 模块:请参阅 https://github.com/FasterXML/jackson-datatype-json-org 。要注册模块,您可以使用以下命令:-

 ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JsonOrgModule());

关于java - 尽管已提及该属性,但出现无法识别的属性异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32692205/

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