Working with Complex Data Types, Part 4
|
Related Reading
Java and SOAP |
This is the last in a series of book excerpts from Java and SOAP on working with complex data types. In this excerpt, learn about returning custom types, using a stock market example.
Returning Custom Types
It's equally useful (and equally common) to return custom types from service method calls. We can enhance our trading service by offering a method that takes a single stock symbol as a parameter and returns its high and low trading prices for the day. The classes HighLow_ServerSide and HighLow_ClientSide represent the high/low prices on the server and client, respectively.
package javasoap.book.ch5;
public class HighLow_ServerSide {
public float _high;
public float _low;
public HighLow_ServerSide( ) {
}
public HighLow_ServerSide (float high, float low) {
setHigh(high);
setLow(low);
}
public float getHigh( ) {
return _high;
}
public void setHigh(float high) {
_high = high;
}
public float getLow( ) {
return _low;
}
public void setLow(float low) {
_low = low;
}
}
package javasoap.book.ch5;
public class HighLow_ClientSide {
public float _high;
public float _low;
public String toString( ) {
return "High: " + _high +
" Low: " + _low;
}
public HighLow_ClientSide( ) {
}
public float getHigh( ) {
return _high;
}
public void setHigh(float high) {
_high = high;
}
public float getLow( ) {
return _low;
}
public void setLow(float low) {
_low = low;
}
}
|
In This Series
Working with Complex Data Types, Part 3
Working with Complex Data Types, Part 2
Working with Complex Data Types, Part 1 |
The server-side class includes a parameterized constructor as a convenience for creating the return value; the client-side class includes a toString( ) method to make it easy for our client application to display the contents of the object after it's returned from the server. Let's add a new method to the BasicTradingService class called getHighLow( ). That method takes a single string parameter for the stock symbol and returns an instance of HighLow_ServerSide. Here's the class with its new method, with the unchanged code omitted:
package javasoap.book.ch5;
public class BasicTradingService {
public BasicTradingService( ) {
}
. . .
. . .
public HighLow_ServerSide getHighLow(String stock) {
// retrieve the high and low for the specified stock
return new HighLow_ServerSide((float)110.375,
(float)109.5);
}
}