我正在使用jackson库(2.5.2)并尝试反序列化一些实际上具有CDATA部分的XML。
Jackson 2.5似乎增加了对CDATA的支持。
这是我的班级有CDATA:
public class Certificate {
@JacksonXmlProperty(localName = "name", isAttribute = true)
private String name;
@JacksonXmlCData
private String data;
@JacksonXmlProperty(localName = "date-added", isAttribute = true)
@JsonFormat(pattern = "EEE MMM d HH:mm:ss z yyyy")
private Date dateAdded;
@JsonFormat(pattern = "EEE MMM d HH:mm:ss z yyyy")
@JacksonXmlProperty(localName = "date-expires", isAttribute = true)
private Date expirationDate;
public void setData(String data) {
this.data = data;
}
public String getData() {
return data;
}
public String getName() {
return name;
}
public Date getDateAdded() {
return dateAdded;
}
public void setDateAdded(Date dateAdded) {
this.dateAdded = dateAdded;
}
}
我得到以下异常。有什么方法可以用杰克逊对CDATA进行反序列化吗?
似乎使用JacksonXmlText工作正常,但是当我需要序列化时,我需要将值放在CDATA而不是文本中。
这是我第一次使用杰克森,所以我可能会遗漏一些明显的东西。
谢谢,
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" (class com.company.model.Certificate),
not marked as ignorable (4 known properties: "date-added", "data", "name", "date-expires"])
at [Source: com.ctc.wstx.sr.ValidatingStreamReader@3895ed38; line: 23, column: 36] (through reference chain:
com.company.model.Configuration["partner"]->java.util.HashSet[0]->com.company.model.PartnerConfiguration["config"]
->java.util.HashSet[0]->com.company.model.HostConfiguration["cert"]->java.util.HashSet[0]->com.company.model.Certificate[""])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:817)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:958)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1324)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1302)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:249)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
at com.fasterxml.jackson.dataformat.xml.deser.WrapperHandlingDeserializer.deserialize(WrapperHandlingDeserializer.java:120)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
at com.fasterxml.jackson.dataformat.xml.deser.WrapperHandlingDeserializer.deserialize(WrapperHandlingDeserializer.java:120)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
at com.fasterxml.jackson.dataformat.xml.deser.WrapperHandlingDeserializer.deserialize(WrapperHandlingDeserializer.java:120)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3534)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1936)
at com.fasterxml.jackson.dataformat.xml.XmlMapper.readValue(XmlMapper.java:196)
at com.fasterxml.jackson.dataformat.xml.XmlMapper.readValue(XmlMapper.java:171)
答案 0 :(得分:2)
使用两个注释:
@JacksonXmlCData
@JacksonXmlText
它可以解决问题。那很好!