Note that there are some explanatory texts on larger screens.

plurals
  1. POjava.util.Map / HashMap JAXB to JSON with Jackson - com.fasterxml.jackson.databind.JsonMappingException: Failed to narrow content type
    primarykey
    data
    text
    <p>while trying to de-serialize class with Map/HashMap property from json message annotated with JAXB annotations I am getting</p> <pre><code>com.fasterxml.jackson.databind.JsonMappingException: Failed to narrow content type [map type; class java.util.HashMap, [simple type, class java.lang.String] -&gt; [simple type, class blog.map.generated.Address]] with content-type annotation (blog.map.generated.Customer$AddressMap): Class blog.map.generated.Customer$AddressMap is not assignable to blog.map.generated.Address </code></pre> <p>My json message:</p> <pre><code>{"addressMap":{"shipping":{"street":"2 B Road"},"billing":{"street":"1 A Street"}}} </code></pre> <p>My JAXB annotated class:</p> <pre><code>@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "customer", propOrder = { "addressMap" }) public class Customer { @XmlElement(required = true, type = Customer.AddressMap.class) protected HashMap&lt;String, Address&gt; addressMap; public HashMap&lt;String, Address&gt; getAddressMap() { return addressMap; } public void setAddressMap(HashMap&lt;String, Address&gt; value) { this.addressMap = value; } @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "entry" }) public static class AddressMap { protected List&lt;Customer.AddressMap.Entry&gt; entry; public List&lt;Customer.AddressMap.Entry&gt; getEntry() { if (entry == null) { entry = new ArrayList&lt;Customer.AddressMap.Entry&gt;(); } return this.entry; } @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "key", "value" }) public static class Entry { protected java.lang.String key; protected blog.map.generated.Address value; public java.lang.String getKey() { return key; } public void setKey(java.lang.String value) { this.key = value; } public blog.map.generated.Address getValue() { return value; } public void setValue(blog.map.generated.Address value) { this.value = value; } } } } </code></pre> <p>I tried to avoid this problem by creating custom XmlAdapter for HashMap property as follows:</p> <pre><code>public class MapAdapter extends XmlAdapter&lt;MapAdapter.AdaptedMap, Map&lt;String, Address&gt;&gt; { public static class AdaptedMap { public List&lt;Entry&gt; entry = new ArrayList&lt;Entry&gt;(); } public static class Entry { public String key; public Address value; } @Override public Map&lt;String, Address&gt; unmarshal(AdaptedMap adaptedMap) throws Exception { Map&lt;String, Address&gt; map = new HashMap&lt;String, Address&gt;(); for(Entry entry : adaptedMap.entry) { map.put(entry.key, entry.value); } return map; } @Override public AdaptedMap marshal(Map&lt;String, Address&gt; map) throws Exception { AdaptedMap adaptedMap = new AdaptedMap(); for(Map.Entry&lt;String, Address&gt; mapEntry : map.entrySet()) { Entry entry = new Entry(); entry.key = mapEntry.getKey(); entry.value = mapEntry.getValue(); adaptedMap.entry.add(entry); } return adaptedMap; } } </code></pre> <p>That works BUT creates DIFFERENT MESSAGE than I need to deserialize:</p> <pre><code>{"addressMap":{"entry":[{"key":"shipping","value":{"street":"2 B Road"}},{"key":"billing","value":{"street":"1 A Street"}}]}} </code></pre> <p>Which is somewhat logical as it was converted to List of key-value pairs.</p> <p>When I delete that "Map -> List of Key-Value pairs conversion" than IT WORKS!</p> <pre><code>@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "customer", propOrder = { "addressMap" }) public class Customer { @XmlElement(required = true) protected HashMap&lt;String, Address&gt; addressMap; public HashMap&lt;String, Address&gt; getAddressMap() { return addressMap; } public void setAddressMap(HashMap&lt;String, Address&gt; value) { this.addressMap = value; } } </code></pre> <p>Does anybody know how to deal with such issue using Jackson 2.2.2 and JAXB? OR How to force XJC not to generate Map->List of KV pairs conversion?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload