AnOldSoul
AnOldSoul

Reputation: 4207

Getting unrecognized property exception though the property has been mentioned

I am getting the below exception when I try to read the string message coming in.

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)

This is how im reading the message.

hostEvent = mapper.readValue(message,HostEventObject.class);

This is my HostEventObject class.

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;
}

Below given is my JSON string message.

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

Why am I getting this exception? I have read the similar questions but i am not making mistakes of spelling nor can I ignore this property. Please advice.

Upvotes: 1

Views: 5909

Answers (2)

Amit Kaneria
Amit Kaneria

Reputation: 5808

It seems your POJO is missing a property EventID. You can solve it 2 ways

  1. Add the property in java POJO.

  2. Put the code to ignore above property that can be achieved 2 ways:

    a. Mark the POJO to ignore unknown properties

    @JsonIgnoreProperties(ignoreUnknown = true)
    

    b. Configure ObjectMapper that serializes/De-serializes the POJO/json as below:

    ObjectMapper mapper =new ObjectMapper();    
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    

Upvotes: 0

John McClean
John McClean

Reputation: 5313

MessageData is of type JSONObject which doesn't have a field called EventID. Possibly you meant to put a different class here?

This is your JSON

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

Which requires the datatype for Message Data has an EventID field which is some kind of List / Set / Array or other Collection of Strings . But what we have is -

 @JsonProperty("MessageData")
 public JSONObject MessageData;

UPDATE Looks like there is a Jackson module for JSON org: see https://github.com/FasterXML/jackson-datatype-json-org. To register the module you can use this :-

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

Upvotes: 1

Related Questions