我正在使用杰克逊作为春季启动应用程序的一部分。我正在将JSON转换为Java,我收到了这个错误。我做了一些研究,但我仍然不明白出了什么问题或如何解决它。
这是JSON片段:
"dataBlock": {
"sections": [
{
"info": "",
"prompt": "",
"name": "First Section",
"sequence": 0,
"fields": [],
"gatingConditions": [],
"guid": "480d160c-c34f-4022-97b0-e8a1f28c49ae",
"id": -2
}
],
"prompt": "",
"id": -1,
"name": ""
}
所以这个“dataBlock”元素的Java对象:
public class DataBlockObject {
private int id;
private String prompt;
private String name;
private List<SectionObject> sections;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPrompt() {
return prompt;
}
public void setPrompt(String prompt) {
this.prompt = prompt;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<SectionObject> getSections() {
return sections;
}
public void setSections(List<SectionObject> sections) {
this.sections = sections;
}
}
Section对象是这样的:
public class SectionObject {
private int id;
private String name;
private String prompt;
private String info;
private int sequence;
private List<FieldObject> fields;
private List<GatingConditionObject> gatingConditions;
private String guid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrompt() {
return prompt;
}
public void setPrompt(String prompt) {
this.prompt = prompt;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
public List<FieldObject> getFields() {
return fields;
}
public void setFields(List<FieldObject> fields) {
this.fields = fields;
}
public List<GatingConditionObject> getGatingConditions() {
return gatingConditions;
}
public void setGatingConditions(List<GatingConditionObject> gatingConditions) {
this.gatingConditions = gatingConditions;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
}
所以在我看来,杰克逊会创建一个DataBlockObject,映射明显的elemenet,并创建一个我已经明确标记为List命名部分的数组。 - 就像JSON节目一样。
现在错误是:
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "sections" (class com.gridunity.workflow.bean.json.SectionObject), not marked as ignorable (8 known properties: "gatingConditions", "sequence", "prompt", "fields", "id", "info", "guid", "name"])
现在根据那个错误,我的8个元素中的一个应该被命名为“sections” - 但这不是我的元素之一。我的章节列表显然存在问题,但我无法弄清楚它是什么。
有人可以解释为什么会发生这种情况,特别是看起来我的结构是正确的,以及如何解决这个问题。我在其他帖子上看过这个:
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
但是,由于我了解所有属性,这似乎非常错误。
答案 0 :(得分:0)
看起来JSON本身在一个或多个sections
项中有另一个dataBlock.sections
字段。如果您无法控制JSON对象的构造,则需要在@JsonIgnoreProperties
类上添加SectionObject
注释,以便在JSON对象具有未在POJO,它不会在反序列化过程中抛出错误。
@JsonIgnoreProperties(ignoreUnknown = true)
public class SectionObject {
// class members and methods here
}