views:

29

answers:

1

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 other 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?

+1  A: 

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.

Marc Bollinger
I see... This is a customer-facing web service so I guess I'll have to write separate web methods for each type of parameter.
draganstankovic