views:

1158

answers:

2

I'm pretty sure you can, but in addition to answering the question in the title, could you also explain the pros, cons, caveats, if any, to doing so? I know that you can't return a List, Set, Collection, Map, or any interface, from a WebMethod (which is stupid, IMO, but I don't know what the design reasons were should I should probably withhold judgment).

Thanks for any advice.

-- LES

+2  A: 

You can return arrays, and I have found them useful.

The main reason that collections are a problem is that some languages, such as C, doesn't understand the concept of a collection, so, to remain portable you need to make certain to pass structures that can be represented in the majority of languages.

Besides,I am never a fan of passing collections, as, what comes from the webservice should be fairly static. If you want to add to it, then convert the array to a collection, then pass back an array to the webservice to make changes.

James Black
+1  A: 

James is correct, you can return arrays. You can also return Collection types. The wsdl will define the type as being a list. However, most clients will convert the type into an array (at least that is what I have found C# did with a JAX-WS service).

I used a 3rd party (Apache Axis2) to generate the web service and I came across an odd issue where the wsdl was not being correctly read and the array could not be deserialized correctly by the consumer. It was necessary to create a decorator object, which only contained the array object and have that be returned from the webmethod.

I think that this was just Axis2 not playing well with the .NET web service interpereter, but something to be aware of.

bogertron
is this List a generic List? or the raw List? (in Java, of course)otherwise, how to you communicate to the client that he or she can cast each element in the List to String, Long, MyBean, etc.?
LES2
@LES2: You should use generics, otherwise the contents will all be cast to objects on the client. You will need to re-cast them to their correct type on the client, which should be generated by the web service consumer.
bogertron