How to return an array of complex types in a web service

Going for the full geek posting here, as I mentioned earlier in the week, I’m doing a lot of web services work at the moment. One of the requirements of one of the services is to return an array of complex objects (as opposed to an array of strings for example). The objects get described by the WSDL for serialization but it took me a little while to work out how to actually write the code to return the array…

public class MyService{
  public ComplexType[] getList(String in){
    ArrayList retList = new ArrayList();
    //…
    //Build ArrayList Here
    //…
    return (ComplexType[])retList.toArray(new ComplexType[retList.size()]);
  }
}

I’m sure it’s pretty obvious to most people, but the fact that you have to convert the ArrayList to an array and then also cast it to an array of the correct type was what had confused me. Anyway it’s all working now, which is nice!

Share