我正在尝试从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
在creationDate
课程中寻找CommunicationFile
。 creationDate
位于Communication
级。我正在使用杰克逊2.6.7 API。
答案 0 :(得分:0)
问题中的XML包含一个未包装的Communication
元素集合。必须在注释上指定。 E.g:
@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
的一部分