Jackson XMLWrapper 未能将此 xml 反序列化为 pojo?
问题描述:
考虑下面给定的 xml:
Consider the given xml below:
<?xml version="1.0"?>
<Records>
<Record>
<Prop name="commerce">sku1</Prop>
<Prop name="commerce">sku2</Prop>
<Prop name="commerce">sku3</Prop>
<Prop name="commerce">sku4</Prop>
</Record>
<Record>
<Prop name="commerce">sku10</Prop>
<Prop name="commerce">sku20</Prop>
<Prop name="commerce">sku30</Prop>
<Prop name="commerce">sku40</Prop>
</Record>
</Records>
现在考虑我创建的这些 Pojo(为了简洁起见,删除了 getter 和 setter).
Now consider these Pojos that I have created(removing getter and setters for brevity).
public class Records {
private List<Record> records;
}
public class Record {
private List<Prop> props;
}
public class Prop {
// I want to capture name attribute
private String name;
// But I also want to capture what comes inside Prop element as well. This would then have values like sku1, sku2 etc
private String text;
}
现在我有像这样使用 XMLWrapper 的代码.
Now I have code using XMLWrapper like this.
File file = ResourceUtils.getFile("classpath:sample.xml"); // the above XML is sample.xml
XmlMapper xmlMapper = XmlMapper.builder().build();
Records records = xmlMapper.readValue(file, Records.class); // this line is failing
但我收到这样的错误
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Record" (class com.example.demo.model.xml.Records), not marked as ignorable (one known property: "records"])
答
你必须结合 @JacksonXmlProperty
注释,带有 @JacksonXmlElementWrapper
注释表示要与 Collection
一起使用的包装元素:在这种情况下,可选属性 useWrapping
设置为 false 以显式禁用包装:
You have to combine the @JacksonXmlProperty
annotation with the @JacksonXmlElementWrapper
annotation that indicates the wrapper element to use with a Collection
: in this case the optional property useWrapping
is set to false to explicitly disable the wrapping:
public class Records {
@JacksonXmlProperty(localName = "Record")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Record> records;
}
public class Record {
@JacksonXmlProperty(localName = "Prop")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Prop> props;
}
public class Prop {
//<Prop name="commerce">sku1</Prop> name is an attribute with value commerce
@JacksonXmlProperty(localName= "name", isAttribute = true)
private String name;
//<Prop name="commerce">sku1</Prop> sku1 is the text inside the Prop tags
@JacksonXmlText
private String text;
}