Working with Complex Data Types, Part 2
by Robert EnglanderEditor'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
Working with Complex Data Types, Part 3
Working with Complex Data Types, Part 1 |
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.
|
Related Reading Java and SOAP |
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
URL("http://georgetown:8080/soap/servlet/rpcrouter");
Call call = new Call( );
call.setTargetObjectURI("urn:BasicTradingService");
call.setMethodName("getMostActive");
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++) {
System.out.println(value[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:
ABC
DEF
GHI
JKL
The SOAP envelope returned by this method invocation is pretty straightforward:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getMostActiveResponse
xmlns:ns1="urn:BasicTradingService"
SOAP-ENV:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/">
<return
xmlns:ns2="http://schemas.xmlsoap.org/soap/encoding/"
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>
</return>
</ns1:getMostActiveResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Pages: 1, 2 |
