ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button O'Reilly Book Excerpts: Java and SOAP

Working with Complex Data Types, Part 2

by Robert Englander

Editor's Note: This is the second in a series of excerpts from "Chapter 5: Working with Complex Data Types" of Java and SOAP. This excerpt covers arrays as return values.

In This Series

Working with Complex Data Types, Part 4
This is the last in a series of book excerpts on working with complex data types from Java and SOAP. In this excerpt, learn about returning custom types, using a stock market example.

Working with Complex Data Types, Part 3
The third in a series of excerpts from Java and SOAP, this article excerpt covers passing custom types as parameters.

Working with Complex Data Types, Part 1
In this excerpt on complex data types from Java and SOAP, the authors discuss passing arrays as parameters.

So far we've been passing arrays as parameters. Now let's use an array as the return value of a service method. We'll add a method to our service called getMostActive( ), which returns a String[] that contains the symbols for the most actively traded stocks of the day. Here's the new version of the BasicTradingService class:

package javasoap.book.ch5;
public class BasicTradingService {
   public BasicTradingService(  ) {
   public String[] getMostActive(  ) {
      // get the most actively traded stocks
      String[] actives = { "ABC", "DEF", "GHI", "JKL" };
      return actives;
   public int getTotalVolume(String[] stocks) {
      // get the volumes for each stock from some
      // data feed and return the total
      int total = 345000; 
      return total;
   public String executeTrade(Object[] params) {
      String result;
      try {
         String stock = (String)params[0];
         Integer numShares = (Integer)params[1];
         Boolean buy = (Boolean)params[2];
         String orderType = "Buy";
         if (false == buy.booleanValue(  )) {
            orderType = "Sell";
         result = (orderType + " " + numShares + " of " + stock);
      catch (ClassCastException e) {
         result = "Bad Parameter Type Encountered";
      return result;

Since we're not really calling a data feed, we just stuff a few phony stock symbols into an array and return it.

Java and SOAP

Related Reading

Java and SOAP
By Robert Englander

Go ahead and redeploy the service now. Calling this service method from an Apache SOAP client is simple. There are no parameters to the service method, so we just have to set up the call and invoke it:

package javasoap.book.ch5;
import java.net.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
public class MostActiveClient
  public static void main(String[] args) throws Exception 
    URL url = new   
    Call call = new Call(  );
    Response resp;
    try {
      resp = call.invoke(url, "");
      Parameter ret = resp.getReturnValue(  );
      String[] value = (String[])ret.getValue(  );
      int cnt = value.length;
      for (int i = 0; i < cnt; i++) {
    catch (SOAPException e) {
      System.err.println("Caught SOAPException (" +
                         e.getFaultCode(  ) + "): " +
                         e.getMessage(  ));

We cast the return value of ret.getValue to a String[], since that's the return type we're expecting. In past examples we were able to leave the value as an Object instance because we relied on the object's toString( ) method to display the value. In this case we need to iterate over the array, so it's necessary to cast the value to the appropriate array type. After that, we just find the array length and then loop over the array values, printing each one as we get to it. If you run this example you should see the following output:


The SOAP envelope returned by this method invocation is pretty straightforward:

        xsi:type="ns2:Array" ns2:arrayType="xsd:string[4]">
          <item xsi:type="xsd:string">ABC</item>
          <item xsi:type="xsd:string">DEF</item>
          <item xsi:type="xsd:string">GHI</item>
          <item xsi:type="xsd:string">JKL</item>

Pages: 1, 2

Next Pagearrow