I've tried to create a web method which has parameter of java.lang.Object but I receive error similar to: http://community.jboss.org/message/532500
One guy answered to this saying that: "You can pass only JAXB supported types web method parameters. java.lang.Object cannot be passed as web method parameter."
And indeed, when I changed the parameter type to something othe开发者_Python百科r than Object it worked.
If I cannot pass Object as web method parameter what is the reason behind this and how to handle situations when I want to pass various object types through a single web method?
If I can how should I do it?
A better way to do this would be to use JAXB as the OP outlined in the answer he recieved to an alternate post -- refer here: http://myarch.com/create-jax-ws-service-in-5-minutes.
I'm not a huge JAXB/JAX-WS guy, but: he's probably right.
One pattern we use internally for this--since when you're sending objects across a web service boundary, you're basically flattening them into something that's expressible solely with the vocabulary of your WS-I profile--is to manually serialize whatever object you're attempting to send, and provide some context for the process of deserialization via an enum understood by the caller and the service.
In the following simple example, DemuxEnum
would be an enum that contains values for all of the types you want to send:
MyObject obj = new MyObject();
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(byteOutStream);
objectOut.writeObject(obj);
objectOut.close();
byte[] serializedObject= byteOutStream.toByteArray();
someWSObject.SendObject(DemuxEnum.MyObjectType, serializedObject);
Where you've already set up a web service endpoint that takes (DemuxEnum, byte[])
. For bonus points, you can toss in alternate serializer/deserializer technologies like Apache Thrift or Google ProtocolBuffers.
Edit: The obvious downside of this is that if it's a customer-facing web service, that's just not going to jive. You're probably better off enumerating web methods that have concrete parameters, if that's the case.
精彩评论