I have an XML
document to deserialize with Jackson
:
<root>
<properties>
<property>
<key>k1</key>
<value>v1<value>
</property>
</properties>
</root>
As you can see, /root/properties
looks very much like a map, with each /root/properties/property
resembling a Map.Entry
.
Is there a way to create a POJO
for deserializing this into a Map<String, String>
without needing a custom deserializer?
I was hoping for something like the following, but it didn't work:
@JacksonXmlRootElement(localName = "root")
public class Root {
@JacksonXmlElementWrapper(localName = "properties")
public Map<String, String> properties;
}
The error I get from this is:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
You can use
JsonAnySetter
annotation:Above code prints for above
XML
: