Jackson XML Unable to deserialize list

I try to parse XML like this:

fun main() { val kotlinXmlMapper = XmlMapper(JacksonXmlModule().apply {     setDefaultUseWrapper(false) }).registerKotlinModule()         .configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)         .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)  val value: Feed = kotlinXmlMapper.readValue("""     <feed xmlns="http://www.w3.org/2005/Atom">       <entry>         <id>Test-1</id>         <title>Test</title>         <summary type="html">Test</summary>       </entry>       <entry>         <id>Test-2</id>         <title>Test2</title>         <summary type="html">Test2</summary>       </entry>     </feed> """.trimIndent(), Feed::class.java) println(value)} 

My data classes looks like this:

@JacksonXmlRootElement(localName="feed") data class Feed(         @JacksonXmlElementWrapper(localName="entry")         @JsonProperty("entry")         val entry: List<Entry>)  @JacksonXmlRootElement(localName="entry") data class Entry(val id: String, val title: String, val summary: String) 

I thought I annotate the list element with JacksonXmlElementWrapper and then it should work. but unfortunately not:

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `service.Entry` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('Test-1')  at [Source: (StringReader); line: 3, column: 15] (through reference chain: service.Feed["entry"]->java.util.ArrayList[0]) 

Have anybody an idea/suggestions?

Thanks in advance.

Add Comment
1 Answer(s)

In XML, by default list items are wrapped by an extra node. This node does not represent extra class in POJO model, so, we can skip it. You need to represent in POJO model only item node and specify to Jackson that you want to read payload as a list of these nodes:

val genres = kotlinXmlMapper.readValue<List<Entry>>(json) 

See also:

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.