gpt4 book ai didi

java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException

转载 作者:太空宇宙 更新时间:2023-11-04 10:53:06 24 4
gpt4 key购买 nike

我正在尝试从 xml 创建 java beans。下面是我的 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...

}
------

下面是通信的包装类。此类用于 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类中寻找creationDatecreationDate 位于Communication 类中。我正在使用 jackson 2.6.7 API。

最佳答案

问题中的 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>

jackson 因缺少第二级而感到困惑<Communication>显然/奇怪地考虑creationDate成为 CommunicationFile 的一部分

关于java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47596155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com