Roxy Roxy - 1 month ago 9
Java Question

Jackson Deserialising :com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:

I am trying to deserialise following json into Java object but it is complaining that it is not able to recognise the accesslevel property .Please see below:

{
"Role1": [
{
"path": "/1_order/1_general/customer.comp.json",
"accesslevel": "ENABLED"
},
{
"path": "/1_order/1_general/CustomerComponent.json",
"accesslevel": "DISABLED"
},
{
"path": "/2_help/DummyComponent.json",
"accesslevel": "ENABLED"
}
]
}


Java object looks like:

public class AccessLevelConfigWrapper {

HashMap<String, List<AccessLevelDetails>> listOfRoles;

public AccessLevelConfigWrapper() {

}

public HashMap<String, List<AccessLevelDetails>> getListOfRoles() {
return listOfRoles;
}

public void setListOfRoles(HashMap<String, List<AccessLevelDetails>> listOfRoles) {
this.listOfRoles = listOfRoles;
}

}


AccessLevelDetails:

public class AccessLevelDetails {

@JsonProperty
private String accessLevel;
@JsonProperty
private String path;

public String getAccessLevel() {
return accessLevel;
}

public void setAccessLevel(String accessLevel) {
this.accessLevel = accessLevel;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

}


This is how i am trying to deserialise it :

TypeReference<HashMap<String, List<AccessLevelDetails>>> typeRef = new TypeReference<HashMap<String, List<AccessLevelDetails>>>() {
};
this.accessLevelConfigWrapper = new AccessLevelConfigWrapper();
this.accessLevelConfigWrapper.setListOfRoles(
(new ObjectMapper().readValue(JSONObject.valueToString(this.parentConfigWithPaths), typeRef)));


And i am getting following exception:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "accesslevel" (class com.gatcbiotech.blueberry.gateway.authorization.model.AccessLevelDetails), not marked as ignorable (2 known properties: "accessLevel", "path"])
at [Source: {"CN=it-soft,CN=Groups,DC=intern,DC=gatc-biotech,DC=com":[{"path":"/1_order/1_general/customer.comp.json","accesslevel":"ENABLED"},{"path":"/1_order/1_general/CustomerComponent.json","accesslevel":"DISABLED"},{"path":"/2_help/DummyComponent.json","accesslevel":"ENABLED"}]}; line: 1, column: 122] (through reference chain: java.util.HashMap["CN=it-soft,CN=Groups,DC=intern,DC=gatc-biotech,DC=com"]->java.util.ArrayList[0]->com.gatcbiotech.blueberry.gateway.authorization.model.AccessLevelDetails["accesslevel"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:744)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:915)
at ....

Answer

Fix a typo (accessLevel vs accesslevel). JSON has accesslevel, but in annotation you set accessLevel. They should match, because jackson is case-sensitive when parsing JSON keys.