title: Jersey rest service not returning XMLresponse
body: <p>I am building a rest API using <code>Jersey</code> where <code>XML and JSON</code> outputs are allowed depending on what format client prefers(using <code>Accept</code> header).The service sends the below class as an output which looks like this</p>
  
  <pre><code>@XmlRootElement
  public class ProjectDetails{
  
      private List&lt;Attachment&gt; attachments;
      private Map&lt;String, List&lt;Attachment&gt;&gt; imageCategory;
  
      @XmlTransient
      public List&lt;Attachment&gt; getAttachments() {
          return attachments;
      }
  
      public void setAttachments(List&lt;Attachment&gt; attachments) {
          this.attachments = attachments;
      }
  
      public Map&lt;String, List&lt;Attachment&gt;&gt; getImageCategory() {
          if(attachments == null || attachments.size() == 0){
              return null;
          }
  
          Map&lt;String, List&lt;Attachment&gt;&gt; map = new HashMap&lt;String, List&lt;Attachment&gt;&gt;();
          for (Attachment img : attachments){
              String key = img.getCategory();
              if(BaseUtil.hasText(key)){
                  List&lt;Attachment&gt; values = map.get(key);
                  if (values == null){
                      values = new ArrayList&lt;Attachment&gt;();
                  }
                  values.add(img);
                  map.put(key, values);
              }
          }
  
          this.imageCategory = map ;
          return imageCategory;
      }
  
      public void setImageCategory(Map&lt;String, List&lt;Attachment&gt;&gt; imageCategory) {
          this.imageCategory = imageCategory;
      }
  }
  </code></pre>
  
  <p>I don't want <code>attachments field</code> as an output so marked it with <code>@XmlTransient</code> rather I want to form a <code>Map</code> using the attachments field and send it as an output.</p>
  
  <p>In case of <code>JSON</code> format, I am getting the correct response.But in case of <code>XML</code>, I am not getting any output when I hit the service.</p>
  
  <p>I think it is related to this Map field because if I remove Map field and add some other field like String then I get that field in response.</p>
  
  <p>Please let me know how to resolve this.</p>
  
  <p><strong>Update:</strong>
  After some googling, i found <code>XmlAdapter</code> solution and implemented as below</p>
  
  <pre><code>public class MapAdapter extends
          XmlAdapter&lt;MapAdapter.AdaptedMap, Map&lt;String, List&lt;Attachment&gt;&gt;&gt; {
  
      public static class AdaptedEntry {
          public String key;
          public List&lt;Attachment&gt; value = new ArrayList&lt;Attachment&gt;();
      }
  
      public static class AdaptedMap {
          List&lt;AdaptedEntry&gt; entries = new ArrayList&lt;AdaptedEntry&gt;();
      }
  
      @Override
      public AdaptedMap marshal(Map&lt;String, List&lt;Attachment&gt;&gt; map)
              throws Exception {
          AdaptedMap adaptedMap = new AdaptedMap();
  
          for (Entry&lt;String, List&lt;Attachment&gt;&gt; entry : map.entrySet()) {
              AdaptedEntry adaptedEntry = new AdaptedEntry();
              adaptedEntry.key = entry.getKey();
              adaptedEntry.value = entry.getValue();
              adaptedMap.entries.add(adaptedEntry);
          }
          return adaptedMap;
      }
  
      @Override
      public Map&lt;String, List&lt;Attachment&gt;&gt; unmarshal(AdaptedMap adaptedMap)
              throws Exception {
          List&lt;AdaptedEntry&gt; adapatedEntries = adaptedMap.entries;
          Map&lt;String, List&lt;Attachment&gt;&gt; map = new HashMap&lt;String, List&lt;Attachment&gt;&gt;(
                  adapatedEntries.size());
  
          for (AdaptedEntry adaptedEntry : adapatedEntries) {
              map.put(adaptedEntry.key, adaptedEntry.value);
          }
          return map;
      }
  </code></pre>
  
  <p>And then</p>
  
  <pre><code>@XmlJavaTypeAdapter(MapAdapter.class)
      public Map&lt;String, String&gt; getImageCategory() {
  </code></pre>
  
  <p>But still it's not working..Anything I missed?</p>