Unable to marshal type “java.util.HashMap” while hitting the resource with json

风格不统一 提交于 2019-12-12 03:28:27

问题


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?


回答1:


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            
}



回答2:


You can try putting the Map in a wrapper

@XmlRootElement
public class MapWrapper {

Map<Long, String> yourMap;

}


来源:https://stackoverflow.com/questions/36787165/unable-to-marshal-type-java-util-hashmap-while-hitting-the-resource-with-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!