« Five Things You May Not Know About Me | Main| Oops »

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

Category
Bookmark : del.icio.us  Technorati  Digg This  Add To Furl  Add To YahooMyWeb  Add To Reddit  Add To NewsVine 

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!

Comments

Gravatar Image1 - Umm. I use a slightly simpler method:

class returnClass
public myArray() as whatever...
sub new()
end sub
end class

class actualWebService
public function returnAnArray() as returnClass
' create a new instance of return class here
set returnAnArray = myNewInstanceOfReturnClass
end function
end class


I'm a simple soul me.

Nothing to stop you nesting classes - so that you have a complex multi-value class containing other classes, etc.. We use this to pass back our request class, which then contains profile classes, etc, etc.

---* Bill

    follow me on Twitter

    NO2ID - Stop ID cards and the database state

    Email / MSN / Google Talk matthew.white@fclonline.com
    AIM: whitemx
    Skype: whitemrx

    Hits since 03-Feb-2006