问题
I have an incoming xml from Recurly service with list of transactions. Sometimes it's empty and looks like this:
<transactions type="array">
</transactions>
I need to deserialize this using Jackson. I've tried next mapping
@XmlRootElement(name = "transactions")
public class TransactionObjectListResponse extends ArrayList<TransactionObjectResponse> {
}
where TransactionObjectResponse class for each transaction. It works fine for non-empty collections, but fails when no transactions came. Next message appears:
java.lang.IllegalStateException: Missing name, in state: END_ARRAY
at com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser.getCurrentName(FromXmlParser.java:310)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:289)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:157)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:230)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:207)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
I used XmlMapper directly,
xmlMapper.readValue(responseXml, TransactionObjectListResponse.class);
Response entity structure isn't strict, any help would be appricated. Thanks.
回答1:
I had a similar issue running the 2.2 version of "jackson-dataformat-xml". I took this one library down a version and it worked.
ie. (if you are using Maven)
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.2.3</version>
</dependency>
This is the same version as the 3rd party java recurly library uses - http://search.maven.org/#artifactdetails%7Ccom.ning.billing%7Crecurly-java-library%7C0.1.6%7Cjar
来源:https://stackoverflow.com/questions/18329668/jackson-empty-xml-array-deserialization