Radi
Radi

Reputation: 6584

Return a collection of objects from JAX_WS web service

in my web service I want to return a collection of objects depends on the client request for example (collection of strings , custom class ..) , My code :

@XmlRootElement(name="ResultObject")
public class ResultObject
{
private Object _Contents;  // this object should contain the collection
private int _ErrorCode;

.....
}

but when trying to return an ArrayList or Array of strings the following exception appear:

Dec 30, 2012 5:49:21 PM com.sun.xml.ws.transport.http.servlet.WSServletDelegate doGet SEVERE: caught throwable javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException - with linked exception: [javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.] at com.sun.xml.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:322) at com.sun.xml.ws.message.AbstractMessageImpl.writeTo(AbstractMessageImpl.java:142) at com.sun.xml.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:108) at com.sun.xml.ws.encoding.SOAPBindingCodec.encode(SOAPBindingCodec.java:265) at com.sun.xml.ws.transport.http.HttpAdapter.encodePacket(HttpAdapter.java:320) at com.sun.xml.ws.transport.http.HttpAdapter.access$100(HttpAdapter.java:93) at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:454) at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:244) at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:135) at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:129) at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:160) at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:75) at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

after searching for the solution some of posts advice to use annotations but the _Contents object is dynamic collection and I can't fix it's content .

please advice , Thanks.

Upvotes: 0

Views: 1631

Answers (1)

Arne Burmeister
Arne Burmeister

Reputation: 20604

The _Contents member needs a @XmlAnyElement annotation, but I am not so fimilar with that. Maybe this article will help you: Using @XmlAnyElement to Build a Generic Message

Upvotes: 1

Related Questions