1
我想deserialise以下JSON轉換成Java對象,但抱怨它不能夠識別ACCESSLEVEL財產。請參閱以下內容:傑克遜Deserialising:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
{
"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對象的樣子:
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;
}
}
我這是怎麼想deserialise它:
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)));
而且我得到以下例外:
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 ....
你有一個錯字('accessLevel' vs'accesslevel')。 JSON具有'accesslevel',但在註釋中設置'accessLevel'。 – uthark
@uthark:我不相信,我花了1個小時找出解決方案。謝謝你 – Roxy
如果它有效,那麼將我的答案標記爲解決方案。 – uthark