I am trying to create java beans from xml. Below is my xml :
<CommunicationFile count="1">
<Communication>
<creationDate>2017-11-25</creationDate>
<Document>
<objectReference>111111</objectReference>
</Document>
..........
</Communication>
</CommunicationFile>
Here is my Java files. I created exact mapping to variables to the xml using jackson XML property :
@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
. This class is for nested elements in 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:
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. creationDate
is in Communication
Class. I am uing jackson 2.6.7 API's.
The XML in the question has an unwrapped collection of Communication
elements. 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:
<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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.