我正在尝试从xml创建java bean。下面是我的xml:
<CommunicationFile count="1">
<Communication>
<creationDate>2017-11-25</creationDate>
<Document>
<objectReference>111111</objectReference>
</Document>
..........
</Communication>
</CommunicationFile>这是我的Java文件。我使用jackson xml属性创建了变量到XML的精确映射:
@JacksonXmlRootElement(localName = "CommunicationFile")
public final class CommunicationFile {
@JacksonXmlProperty(localName = "count", isAttribute = true)
private String count;
@JacksonXmlElementWrapper(localName = "Communication")
private Communication[] communication;
..Standard setter and getters...
}
------下面是Communication的包装类。这个类用于CommunicatonFile中的嵌套元素。
public final class Communication {
@JacksonXmlProperty(localName = "creationDate")
private String creationDate;
@JacksonXmlElementWrapper(localName = "Document")
private Document document;
.. standard setter and getters
}尝试使用jackson API将xml映射到pojo:
XMLInputFactory f = XMLInputFactory.newInstance();
XMLStreamReader sr = f.createXMLStreamReader(new FileInputStream("billing.xml"));
XmlMapper mapper = new XmlMapper();
sr.next(); // to point to <root>
sr.next(); // to point to root-element under root
CommunicationFile comm = mapper.readValue(sr, CommunicationFile.class);获取以下错误:
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "creationDate" (class com.pm.communication.CommunicationFile), not marked as ignorable (2 known properties: "Communication", "count"])
at [Source: com.ibm.xml.xlxp2.api.wssec.WSSXMLInputFactory$WSSStreamReaderProxy@981206cc; line: -1, column: -1] (through reference chain: com.pm.communication.CommunicationFile["creationDate"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:839)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1045)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1352)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1330)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:264)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3708)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2071)
at com.fasterxml.jackson.dataformat.xml.XmlMapper.readValue(XmlMapper.java:205)
at com.fasterxml.jackson.dataformat.xml.XmlMapper.readValue(XmlMapper.java:180)
at com.pm.docmaker.tpd.Parser.main(Parser.java:25)我不确定为什么jackson要在CommunicationFile类中寻找creationDate。creationDate在Communication类中。我使用的是jackson 2.6.7的API。
发布于 2017-12-02 22:45:57
问题中的XML有一个未包装的Communication元素集合。这必须在注释中指定。例如:
@JacksonXmlElementWrapper(localName = "Communication", useWrapping = false)
private Communication[] communication;基于当前的注释,XML应该是:
<CommunicationFile count="1">
<Communication>
<Communication>
<creationDate>2017-11-25</creationDate>
...
</Communication>
<Communication>
<creationDate>2018-07-11</creationDate>
...
</Communication>
</Communication>杰克逊对缺少第二级<Communication>感到困惑,他显然/奇怪地认为creationDate是CommunicationFile的一部分
https://stackoverflow.com/questions/47596155
复制相似问题