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 написал:
>
> On Wed, Jul 10, 2019 at 4:55 PM Dmitriy Grigoryev <[email protected] 
> <javascript:>> wrote:
>
>> 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 ?
>>
>>
> 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 +- 
>  
>

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/0daf3268-e486-4ca5-912e-32c1a3913e61%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to