Can I use an object type as an @WebMethod parameter?

I tried to create a web method that has a java.lang.Object parameter, but I get an error similar to: http://community.jboss.org/message/532500

One guy answered this, saying: "You can only pass parameters to the web method of the supported JAXB types. Java.lang.Object cannot be passed as a parameter to the web method.

And indeed, when I changed the type of the parameter to something other than Object, it worked.

If I cannot pass an object as a parameter of a web method, what is the reason for this and how to handle situations when I want to pass different types of objects using one web method?

If I can, how can I do this?

+3
source share
2 answers

I am not a big JAXB / JAX-WS guy, but: he is probably right.

One template that we use internally for this is that when you send objects across the border of a web service, you basically smooth them into something that is expressed solely in the dictionary of your WS-I profile - this is to manually serialize any object, which you are trying to send, and provide some context for the deserialization process through an enumeration that is clear to the caller and the service.

The following simple example DemuxEnumwill be an enumeration that contains values ​​for all types that 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);

-, (DemuxEnum, byte[]). /, Apache Thrift Google ProtocolBuffers.

. , -, . , , -, , .

+2

Source: https://habr.com/ru/post/1753443/


All Articles