title: EclipseLink Throwing Exception?
body: <p>i'm working on a project based on jersey implementation of REST,and i i'm using JPA for persistence,spring IoC for DI.Now i have some objects that must be marshalled with JAXB (MOXy).Following step by step <a href="https://stackoverflow.com/questions/3941479/jaxb-how-to-marshall-map-into-keyvalue-key">this tuto</a> i tried to get things working but unfortunately i haven't succeeded yet.The folling are my classes,if you can help please:</p>
  
  <p>Root classe :</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.persistent.entity;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  
  @XmlAccessorType(XmlAccessType.NONE)
  @XmlRootElement
  public class Bar {  
      private Map&lt;String,String&gt; mapbar;
  
      public Bar() {
  
      }
  
      public Bar(Map&lt;String,String&gt; map ){
          this.mapbar = map;
      }
  
      @XmlJavaTypeAdapter(MapAdapter.class)
      public Map&lt;String, String&gt; getMapbar() {
          return mapbar;
      }
  
      public void setMapbar(Map&lt;String, String&gt; map) {
          this.mapbar = map;
      }
  }
  </code></pre>
  
  <p>MapAdapter class :</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.persistent.entity;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Map.Entry;
  
  //import javax.xml.bind.JAXBContext;
  import javax.xml.bind.annotation.adapters.XmlAdapter;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  public class MapAdapter extends XmlAdapter&lt;AdaptedMap, Map&lt;String, String&gt;&gt;  {
  
      @Override
      public AdaptedMap marshal(Map&lt;String, String&gt; map) throws Exception {
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document document = db.newDocument();
          Element rootElement = document.createElement("bar");
          document.appendChild(rootElement);
  
          for(Entry&lt;String,String&gt; entry : map.entrySet()) {
              Element mapElement = document.createElement(entry.getKey());
              mapElement.setNodeValue(entry.getValue());
              rootElement.appendChild(mapElement);
          }
  
          AdaptedMap adaptedMap = new AdaptedMap();
          adaptedMap.setValue(document);
          return adaptedMap;
      }
  
      @Override
      public Map&lt;String, String&gt; unmarshal(AdaptedMap adaptedMap) throws Exception {
          Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
          Element rootElement = (Element) adaptedMap.getValue();
          NodeList childNodes = rootElement.getChildNodes();
          for(int x=0,size=childNodes.getLength(); x&lt;size; x++) {
              Node childNode = childNodes.item(x);
              if(childNode.getNodeType() == Node.ELEMENT_NODE) {
                  map.put(childNode.getLocalName(), childNode.getTextContent());
              }
          }
          return map;
      }  
  }
  </code></pre>
  
  <p>AdaptedMap class :</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.persistent.entity;
  
  import javax.xml.bind.annotation.XmlAnyElement;
  
  //import org.w3c.dom.Document;
  
  public class AdaptedMap {
  
      private Object value;
  
      @XmlAnyElement
      public Object getValue() {
          return value;
      }
  
      public void setValue(Object value) {
          this.value = value;
      }    
  }
  </code></pre>
  
  <p>and, finally, the resource class :</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.persistent.rest;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import javax.ws.rs.DefaultValue;
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.QueryParam;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.Marshaller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Scope;
  import org.springframework.stereotype.Component;
  
  import com.persistent.entity.Bar;
  import com.persistent.entity.BarList;
  import com.persistent.entity.Pie;
  import com.persistent.entity.Vente;
  import com.persistent.entity.VentesList;
  import com.persistent.entity.PieList;
  import com.persistent.service.VenteService;
  import com.sun.jersey.api.json.JSONWithPadding;
  
  // The Java class will be hosted at the URI path "/myresource"
  @Path("/myresource")
  @Component
  @Scope("request")
  public class MyResource {
  
      @Autowired
      VenteService venteService;
  
  
      @GET
      @Produces("application/x-javascript")
  
      public JSONWithPadding getBarService(@QueryParam("callback")  String callback){
          int i;
  
          VentesList ventes =  venteService.getAll();
          Map&lt;String,String&gt; hmBar = new HashMap&lt;String,String&gt;();
  
    for (i=0;i&lt;ventes.getsimpleVentes().size();i++){
  hmBar.put(ventes.getsimpleVentes().get(i).getProjet(),String.valueOf(
      ventes.getsimpleVentes().get(i).getNbrRsrv()));
  
  }   
  
    Bar ba = new Bar(hmBar);
      return  new JSONWithPadding(ba,callback);
      } 
  }
  </code></pre>
  
  <p>After running this on Tomcat i get this Exception :</p>
  
  <pre><code>GRAVE: "Servlet.service()" pour la servlet Jersey Spring Web Application a généré une exception
  java.io.IOException: Error marshalling JAXB object of type "class com.persistent.entity.Bar".
      at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:145)
      at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:254)
      at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:578)
      at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:502)
      at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:493)
      at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:308)
      at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:314)
      at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:619)
  Caused by: javax.xml.bind.MarshalException
   - with linked exception:
  [Exception [EclipseLink-25003] (Eclipse Persistence Services - 2.3.1.v20111018-r10243): org.eclipse.persistence.exceptions.XMLMarshalException
  Exception Description: An error occurred marshalling the object
  Internal Exception: Exception [EclipseLink-3001] (Eclipse Persistence Services - 2.3.1.v20111018-r10243): org.eclipse.persistence.exceptions.ConversionException
  Exception Description: The object [{Dyar Al Baydae=6, TEST=8, test=5, Dyar Al Bahja=20, Dyar Al Mansour=87}], of class [class java.util.HashMap], could not be converted to [class com.persistent.entity.AdaptedMap].]
      at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:326)
      at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:167)
      at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:143)
      ... 19 more
  </code></pre>