gpt4 book ai didi

java - Jackson 反序列化 :com. fastxml.jackson.databind.exc.UnrecognizedPropertyException:

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

我正在尝试将以下 json 反序列化为 Java 对象,但它提示它无法识别 accesslevel 属性。请参见下文:

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

}

访问级别详细信息:

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

}

这就是我尝试反序列化它的方式:

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 ....

最佳答案

修复拼写错误(accessLevelaccesslevel)。 JSON 具有 accesslevel,但在注释中设置 accessLevel。它们应该匹配,因为 jackson 在解析 JSON 键时区分大小写。

关于java - Jackson 反序列化 :com. fastxml.jackson.databind.exc.UnrecognizedPropertyException:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40423757/

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