Sunday, May 25, 2008

Webservices with return type List<T>

I had written few web service which were returning List. Following are my findings when working with that.

Axis2 did not generate definition of T in wsdl. So to over come that problem I created a dummy method setT(T t) in order to get type definition of T generated in wsdl. Even though this is work around and it did not highlight or resolve the actual problem.

Axis2 generated List return type as anyType Object. Which created problem during client side code generation. The wsdl published the return type as anyType, Client had no way of determining what type of return object to be generated. So to be type safe I changed the implementation from List to T[]. Now while I believe type List will always be generated as anyType, I do think that List should not be generated as anyType. Tools should be smart enough to generate List as ArrayOfSpecificType in wsdl.
I think that is either due to two reasons,
  1. Tools have not caught upto the JDK5
  2. The specific way Java implements Generics, by using erasure, List becomes List which creates problem for the tool as tools have no way of identifying what type of list was that.

There are some other minor problems I faced while using array. I used method toArray(T[] t) to convert my List to Array. The method required me to pass a type T[] t. I created T[] t = new T[?]. I put number 3 - just randomly- to see if it all works. Service worked it generated response and it also appended two more elements named item1 and item2. I was clueless for a while where this sub elements are getting generated. Then it occurred to me the creation of array with three elements is putting it there. Finally, I settled with T[0]. But the problem with that is when result set was 0, client threw error stating unexpected element of type XXXXResponse. I believe this is a bug.
Post a Comment