title: Is it possible for JAXB (in Jersey JAX-RS) to support java.util.HashMap for XML and JSON format
body: <p>I am using the Jersey implementation of JAX-RS to create a RESTful service.  I want the service to support both XML and JSON responses through the very convenient JAXB support built into JAX-RS.  </p>
  
  <p>Everything works wonderfully until I attempt to use a java.util.HashMap (NOTE: you can't use interfaces with JAXB).  It suprised me to discover that JAXB does not have support built in for Maps in XML, although the Jackson JSON JAXB plugin does have support.</p>
  
  <p><strong>First Attempt:</strong></p>
  
  <pre><code>@XmlElement(name = "Links")
  HashMap&lt;String, LinkDTO&gt; links = new HashMap&lt;String, LinkDTO&gt;();
  </code></pre>
  
  <p><strong>XML Output Empty:</strong></p>
  
  <pre><code>&lt;Links /&gt;
  </code></pre>
  
  <p><strong>JSON Output Correct:</strong></p>
  
  <pre><code>"Links": {
      "status": {
          ...
      },
      "cancel": {
          ...
      }
  }
  </code></pre>
  
  <p>So to fix the problem I figured that a custom XmlAdapter would do the trick, but that broke the JSON serialization.</p>
  
  <p><strong>Second Attempt:</strong></p>
  
  <pre><code>@XmlJavaTypeAdapter(HashMapAdapter.class)
  @XmlElement(name = "Links")
  HashMap&lt;String, LinkDTO&gt; links = new HashMap&lt;String, LinkDTO&gt;();
  
  public class HashMapAdapter extends XmlAdapter&lt;MapElement, HashMap&lt;String, LinkDTO&gt;&gt; {
      public HashMap&lt;String, LinkDTO&gt; unmarshal(ArrayList&lt;MapEntryType&gt; jaxbDTO) throws Exception {
          ... mapping code ...
      }
      public MapElement marshal(HashMap&lt;String, LinkDTO&gt; map) throws Exception {
          ... mapping code ...
      }
  }
  
  public static class MapElement {
      @XmlElement(name = "Link")
      public List&lt;MapEntryType&gt; entries = new ArrayList&lt;MapEntryType&gt;();
  }
  
  public class MapEntryType {
      @XmlElement(name = "Key")
      public String key;
  
      @XmlElement(name = "Value")
      public RESTfulLinkDTO value;
  }
  </code></pre>
  
  <p><strong>XML Output Now Acceptable:</strong></p>
  
  <pre><code>&lt;Links&gt;
      &lt;Link&gt;
          &lt;Key&gt;status&lt;/Key&gt;
          &lt;Value&gt; ... &lt;/Value&gt;
      &lt;/Link&gt;
      &lt;Link&gt;
          &lt;Key&gt;cancel&lt;/Key&gt;
          &lt;Value&gt; ... &lt;/Value&gt;
      &lt;/Link&gt;
  &lt;/Links&gt;
  </code></pre>
  
  <p><strong>JSON Output no longer a valid JavaScript Map:</strong></p>
  
  <pre><code>"Links": {
      "Link": [
          {
              "Key": "status",
              "Value": {
                  ...
              }
          },
          {
              "Key": "cancel",
              "Value": {
                  ...
              }
          }
      ]
  }   
  </code></pre>
  
  <p>Is there any possible way to get the HashMap data to output as XML without breaking the JSON output?  I don't really care what the specific layout for the XML is as long as all of the data is present.  I would prefer a JAXB solution to this issue, but if there is a Jersey configuration that will fix it I would settle for that.</p>