Jackson empty xml array deserialization
I have an incoming xml from a Recurly service with a list of transactions. Sometimes it is empty and looks like this:
<transactions type="array">
</transactions>
I need to deserialize this using Jackson. I have tried the following mapping
@XmlRootElement(name = "transactions")
public class TransactionObjectListResponse extends ArrayList<TransactionObjectResponse> {
}
where is the TransactionObjectResponse class for each transaction. It works fine for non-empty collections, but doesn't work when there were no transactions. The following 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 have used XmlMapper directly,
xmlMapper.readValue(responseXml, TransactionObjectListResponse.class);
The structure of the essence of the answer is not strict, any help would be appreciated. Thank.
source to share
I had a similar problem with the "jackson-dataformat-xml" version 2.2. I took this library by version and it worked.
so (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 third party java library recurly using - http://search.maven.org/#artifactdetails%7Ccom.ning.billing%7Crecurly-java-library%7C0.1.6%7Cjar
source to share