[英]com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
I am trying to create java beans from xml. 我正在尝试从xml创建Java bean。 Below is my xml : 以下是我的xml:
<CommunicationFile count="1">
<Communication>
<creationDate>2017-11-25</creationDate>
<Document>
<objectReference>111111</objectReference>
</Document>
..........
</Communication>
</CommunicationFile>
Here is my Java files. 这是我的Java文件。 I created exact mapping to variables to the xml using jackson XML property : 我使用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...
}
------
Below is the wrapper class for Communication
. 以下是Communication
的包装器类。 This class is for nested elements in CommunicatonFile
. 此类用于CommunicatonFile
嵌套元素。
public final class Communication {
@JacksonXmlProperty(localName = "creationDate")
private String creationDate;
@JacksonXmlElementWrapper(localName = "Document")
private Document document;
.. standard setter and getters
}
Trying to map xml to pojo using jackson APIs: 尝试使用杰克逊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);
Getting below error : 出现以下错误:
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)
I am not sure why jackson
is looking for creationDate
in CommunicationFile
class. 我不确定jackson
为什么要在CommunicationFile
类中寻找creationDate
。 creationDate
is in Communication
Class. creationDate
在Communication
类中。 I am uing jackson 2.6.7 API's. 我正在使用杰克逊2.6.7 API。
The XML in the question has an unwrapped collection of Communication
elements. 问题中的XML具有未包装的Communication
元素集合。 This has to be specified on the annotation. 这必须在注释上指定。 Eg: 例如:
@JacksonXmlElementWrapper(localName = "Communication", useWrapping = false)
private Communication[] communication;
Based on the current annotation the XML should be: 基于当前注释,XML应该是:
<CommunicationFile count="1">
<Communication>
<Communication>
<creationDate>2017-11-25</creationDate>
...
</Communication>
<Communication>
<creationDate>2018-07-11</creationDate>
...
</Communication>
</Communication>
Jackson is confused with the lack of a second level <Communication>
and apparently/bizarrely considers creationDate
to be part of CommunicationFile
杰克逊由于缺乏第二级<Communication>
而感到困惑,显然/很显然地认为creationDate
是CommunicationFile
一部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.