Jackson XMLWrapper 未能将此 xml 反序列化为 pojo?
Jackson XMLWrapper failing to deserialize this xml into pojo?
考虑下面给定的 xml:
<?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)。
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 的代码。
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
annotation with the @JacksonXmlElementWrapper
注释与 Collection
结合使用:在这种情况下,可选的 属性 useWrapping
设置为 false 明确禁用包装:
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;
}
即使没有 XML 注释(在大多数情况下),这个解决方案似乎对我有用。为后代发帖。
第 1 步:禁用换行。
File file = ResourceUtils.getFile("classpath:sample.xml");
XmlMapper xmlMapper = XmlMapper.builder().defaultUseWrapper(false).build();
Records records = xmlMapper.readValue(file, Records.class);
第 2 步:使用 JsonProperty 注释更新 pojo(唯一的例外是使用 @JacksonXmlText 获取内部值。注意为简洁起见删除 setter 和 getter。
public class Records {
@JsonProperty("Record")
private List<Record> records;
}
public class Record {
@JsonProperty("Prop")
private List<Prop> props;
}
public class Prop {
@JsonProperty("name")
private String name;
@JacksonXmlText
private String text;
}
以上对我有用。
考虑下面给定的 xml:
<?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)。
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 的代码。
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
annotation with the @JacksonXmlElementWrapper
注释与 Collection
结合使用:在这种情况下,可选的 属性 useWrapping
设置为 false 明确禁用包装:
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;
}
即使没有 XML 注释(在大多数情况下),这个解决方案似乎对我有用。为后代发帖。
第 1 步:禁用换行。
File file = ResourceUtils.getFile("classpath:sample.xml");
XmlMapper xmlMapper = XmlMapper.builder().defaultUseWrapper(false).build();
Records records = xmlMapper.readValue(file, Records.class);
第 2 步:使用 JsonProperty 注释更新 pojo(唯一的例外是使用 @JacksonXmlText 获取内部值。注意为简洁起见删除 setter 和 getter。
public class Records {
@JsonProperty("Record")
private List<Record> records;
}
public class Record {
@JsonProperty("Prop")
private List<Prop> props;
}
public class Prop {
@JsonProperty("name")
private String name;
@JacksonXmlText
private String text;
}
以上对我有用。