Jackson XML 反序列化 - 保存无法识别的字段
Jackson XML Deserialization - Save an Unrecognized Field
我正在使用 Jackson 反序列化服务器返回的 XML 文件。我收到带有错误消息 "Unrecognized field "" ".
的 UnrecognizedPropertyException
有问题的XML:
<Identifiers>
<Identifier type="Organisational Study">Q2736g</Identifier>
<Identifier type="NCT">NCT00109200</Identifier>
</Identifiers>
堆栈跟踪:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" (class sloan.scraper.Trial$Identifier), not marked as ignorable (3 known properties: , "data2", "type", "data"])
at [Source: java.io.StringReader@28c2f254; line: 1, column: 547] (through reference chain: sloan.mit.cortellisScraper.Trial["Identifiers"]->sloan.mit.cortellisScraper.Identifier[""])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:579)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:672)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:906)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:328)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:147)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:107)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:308)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2796)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1942)
at sloan.mit.cortellisScraper.Main.fetchJson(Main.java:173)
at sloan.mit.cortellisScraper.Main.main(Main.java:64)
如何使用Jackson来保存无法识别的字段?
无法保存无法识别的字段,因为 Jackson 不知道将其映射到何处。
2 件事:
1.) Check/Modify 您的对象模型并确保它与 XML 匹配。
2.) 用 @JsonIgnoreProperties(ignoreUnknown=true)
注释您的 class 以防止发生此错误。但是,无法识别的字段的值将丢失。
我正在使用 Jackson 反序列化服务器返回的 XML 文件。我收到带有错误消息 "Unrecognized field "" ".
的 UnrecognizedPropertyException有问题的XML:
<Identifiers>
<Identifier type="Organisational Study">Q2736g</Identifier>
<Identifier type="NCT">NCT00109200</Identifier>
</Identifiers>
堆栈跟踪:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" (class sloan.scraper.Trial$Identifier), not marked as ignorable (3 known properties: , "data2", "type", "data"])
at [Source: java.io.StringReader@28c2f254; line: 1, column: 547] (through reference chain: sloan.mit.cortellisScraper.Trial["Identifiers"]->sloan.mit.cortellisScraper.Identifier[""])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:579)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:672)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:906)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:328)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:147)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:107)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:308)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2796)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1942)
at sloan.mit.cortellisScraper.Main.fetchJson(Main.java:173)
at sloan.mit.cortellisScraper.Main.main(Main.java:64)
如何使用Jackson来保存无法识别的字段?
无法保存无法识别的字段,因为 Jackson 不知道将其映射到何处。
2 件事:
1.) Check/Modify 您的对象模型并确保它与 XML 匹配。
2.) 用 @JsonIgnoreProperties(ignoreUnknown=true)
注释您的 class 以防止发生此错误。但是,无法识别的字段的值将丢失。