title: How to marshal unmarshal a Map using Jersey
body: <p>I have seen a lot of examples where a Map is passed as an object in a class and annotated with a custom XMLJavaAdapter which is used for marshalling/unmarshalling the Map. But I am trying to pass a Map itself as the requestedEntity in a POST request and the response also as a Map and not a class containing a Map for which I can see numerous solutions..</p>
  
  <p>Input Class(Requested Entity):
  <strong>GenericMap.java</strong></p>
  
  <pre><code>import java.util.HashMap;
  import javax.xml.bind.annotation.XmlRootElement;
  
  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  
  @XmlRootElement
  @XmlJavaTypeAdapter(GenericMapAdapter.class)
  public class GenericMap&lt;K,V&gt; extends HashMap&lt;K,V&gt; {
  
  }
  </code></pre>
  
  <p>GenericMapAdapter.java</p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Map;
  import java.util.Map.Entry;
  
  import javax.xml.bind.annotation.adapters.XmlAdapter;
  
  
   public class GenericMapAdapter&lt;K, V&gt; extends XmlAdapter&lt;MapType&lt;K,V&gt;, Map&lt;K,V&gt;&gt; {
  
      @Override
      public MapType marshal(Map&lt;K,V&gt;  map) throws Exception {
          MapType&lt;K,V&gt; mapElements = new MapType&lt;K,V&gt;();        
  
          for (Map.Entry&lt;K, V&gt; entry : map.entrySet()){
              MapElementsType&lt;K,V&gt; mapEle = new MapElementsType&lt;K,V&gt;      (entry.getKey(),entry.getValue());
              mapElements.getEntry().add(mapEle);
          }
          return mapElements;
      }
  
      @Override
      public Map&lt;K, V&gt; unmarshal(MapType&lt;K,V&gt; arg0) throws Exception {
          Map&lt;K, V&gt; r = new HashMap&lt;K, V&gt;();
          K key;
          V value;
          for (MapElementsType&lt;K,V&gt; mapelement : arg0.getEntry()){
              key =mapelement.key;
              value = mapelement.value;
             r.put(key, value);
          }
          return r;
  
      }
  }
  </code></pre>
  
  <p><strong>MapType.java</strong></p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  public class MapType&lt;K, V&gt; {
  
      private List&lt;MapElementsType&lt;K, V&gt;&gt; entry = new ArrayList&lt;MapElementsType&lt;K, V&gt;&gt;();
  
      public MapType() {
      }
  
      public MapType(Map&lt;K, V&gt; map) {
          for (Map.Entry&lt;K, V&gt; e : map.entrySet()) {
              entry.add(new MapElementsType&lt;K, V&gt;(e.getKey(),e.getValue()));
          }
      }
  
      public List&lt;MapElementsType&lt;K, V&gt;&gt; getEntry() {
          return entry;
      }
  
      public void setEntry(List&lt;MapElementsType&lt;K, V&gt;&gt; entry) {
          this.entry = entry;
      }
  }
  </code></pre>
  
  <p><strong>MapElementsType.java</strong></p>
  
  <pre><code>import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  
  
  @XmlRootElement
  public class MapElementsType&lt;K,V&gt;
  {
    @XmlElement public K  key;
    @XmlElement public V value;
  
    public MapElementsType() {} //Required by JAXB
  
    public MapElementsType(K key, V value)
    {
      this.key   = key;
      this.value = value;
    }
  
  }
  </code></pre>
  
  <p>When I make genericmap as an member variable of a class and annotate it with GenericMapAdapter, it works fine. But, I want GenericMap itself to be passed as input requested entity. And when i try that, i see an empty xml request in my logs and 400 Bad Request :</p>
  
  <p>
  </p>