0

I have written a RESTful resource method which produces application/json and application/xml which is defined at class level and below is my method:

@GET
@Path("testing/getNames")
public Map<Long, String> getNames(@QueryParam("list") List<Long> list) {
    // return invoking ejb method and return map            
}

When i hit this resource with json produces, it's giving the response back but for format=format.xml i am getting the following exception:

Caused by: javax.xml.bind.MarshalException   
 - with linked exception:   
[com.sun.istack.SAXException2: unable to marshal type "java.util.HashMap" as an element because it is missing an @XmlRootElement annotation]  
        at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:326)   
        at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:251)  
        at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:110)  
        at org.glassfish.jersey.jaxb.internal.AbstractRootElementJaxbProvider.writeTo(AbstractRootElementJaxbProvider.java:190)  
        at org.glassfish.jersey.jaxb.internal.AbstractRootElementJaxbProvider.writeTo(AbstractRootElementJaxbProvider.java:169)
        ... 81 more
Caused by: com.sun.istack.SAXException2: unable to marshal type "java.util.HashMap" as an element because it is missing an @XmlRootElement annotation
        at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:249)
        at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:338)
        at com.sun.xml.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBody(ArrayReferenceNodeProperty.java:118)
        at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:159)
        at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:360)
        at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:593)
        at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:341)
        at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494)
        at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323)

Any suggestion on this?

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
moh
  • 1,426
  • 2
  • 16
  • 43
  • Have you taken a look at this: http://stackoverflow.com/questions/20538149/jaxb-unmarshal-xml-elements-to-hashmap – Ben Green Apr 22 '16 at 08:22

2 Answers2

0

Added MapWrapper class

@XmlRootElement(name="MapWrapper")
    public class MapWrapper implements Serializable{

        private static final long serialVersionUID = 1L;


        private Map<Long, String> yourMap;


        public Map<Long, String> getYourMap() {
            return yourMap;
        }

        public void setYourMap(Map<Long, String> yourMap) {
            this.yourMap= yourMap;
        }

    }

and used in the resource method

@GET
@Path("testing/getNames")
public MapWrapper getNames(@QueryParam("list") List<Long> list) {
    // return invoking ejb method and return map            
}
moh
  • 1,426
  • 2
  • 16
  • 43
-1

You can try putting the Map in a wrapper

@XmlRootElement
public class MapWrapper {

Map<Long, String> yourMap;

}
BadChanneler
  • 1,450
  • 1
  • 12
  • 20
  • no i can't do this..IDE is giving an error "The annotation @XmlRootElement is disallowed for this location" – moh Apr 22 '16 at 09:30