XmlMapper. Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token

55 views
Skip to first unread message

Dmitriy Grigoryev

unread,
Jul 10, 2019, 7:55:08 PM7/10/19
to jackson-user
Hi!
I have the following formatted xml fragment of some service response: 

...
   
<items>
</items>
...


Note! There is linebreak between start and closing elements. 

And I have the following corresponding code fragment:

@JacksonXmlProperty(localName = "item")
@JacksonXmlElementWrapper(localName = "items")
private List<Item> items;

Why I get the following deserialization error ?

Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token

I think that if we have this combination of annotations for a field then it should consider only collection items and nothing else inside the wrapping element. 
Is it bug or feature ?

Tatu Saloranta

unread,
Jul 12, 2019, 1:09:37 AM7/12/19
to jackson-user
It is difficult to say since you are only including snippets and not the whole XML or whole class definition.
If you include both (or maybe even reproduction), it will be easier to see what the problem is.

But the way to troubleshoot things with Jackson XML module is usually to reverse the process: see what kind of output you would get for specific annotated class, and make that match what you expect as input.

-+ Tatu +- 
 

lynxpluto

unread,
Jul 12, 2019, 4:30:51 AM7/12/19
to jackson-user
Hi! It is simple to reproduce:

Jackson version and jackson-dataformat-xml version: 2.9.8

DTO

@JacksonXmlRootElement(localName = "test")
public class TestResult {
   
@JacksonXmlProperty(localName = "string")
   
@JacksonXmlElementWrapper(localName = "strings")
   
private List<String> strings;

   
public TestResult() {
   
}

   
public List<String> getStrings() {
       
return strings;
   
}

   
public void setStrings(List<String> strings) {
       
this.strings = strings;
   
}
}

A test method

void testElementWrapperWithNoElements() throws IOException {
   
ObjectMapper mapper = new XmlMapper();
   
String xml = "<test><strings></strings></test>";
   
TestResult res = mapper.readValue(xml, TestResult.class);
   
assert res != null && res.getStrings() == null;
}

Expected null value for the collection field but got an exception

com.fasterxml.jackson.databind.exc.MismatchedInputException: 
Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token
 at [Source: (StringReader); line: 1, column: 16] (through reference chain: com.acme.TestResult["strings"])
at com.acme.XmlSerializationTest.testElementWrapperWithNoElements(XmlSerializationTest.java:258)




пятница, 12 июля 2019 г., 8:09:37 UTC+3 пользователь Tatu Saloranta написал:
Reply all
Reply to author
Forward
0 new messages