Java and SOAP
Java and SOAP provides Java developers with an in-depth look at SOAP (the Simple Object Access Protocol). Of course, it covers the basics: what SOAP is, why it's soared to a spot on the Buzzwords' Top Ten list, and what its features and capabilities are. And it shows you how to work with some of the more common Java APIs in the SOAP world: Apache SOAP and GLUE.
Java and SOAP also discusses interoperability between the major SOAP platforms, including Microsoft's .NET, SOAP messaging, SOAP attachments, message routing, and a preview of the forthcoming AXIS APIs and server. If you're a Java developer who would like to start working with SOAP, this is the book you need to get going.
Chapter 5 Working with Complex
Data Types
In the previous chapter, we created RPC-style services in Java.
Those services dealt only with simple data types. Simple data types may
suffice in many situations, but you'll eventually need to use more complex
data types, like those you're familiar with in your day-to-day Java
programming. In this chapter we'll look at creating services with method
parameters and return values that are arrays and Java beans. We'll hold off on
custom data types until the next chapter, since it's possible that any custom
types you create would use the complex data types we'll be discussing
here.
Passing Arrays as Parameters
Let's face it--arrays are probably the most common complex data
type in programming. They're everywhere, so their use in SOAP is critical. We
covered the details of SOAP arrays back in Chapter 3, so you should be aware
of how arrays are encoded. So let's get right into writing some Java code for
services that use arrays.
We've been working with stock market
examples, so let's stick with that theme. It might be useful to have a service
that returns information about a collection of stock symbols. It might provide
the total volume of shares traded for the day, the average trading price of
those stocks, the number of stocks trading higher for the day, etc. There are
lots of possibilities. Let's start out with a service that returns the total
number of shares traded for the day. The service is called urn:BasicTradingService, and it has a method called getTotalVolume. Here is the Java class that implements
the service:
package javasoap.book.ch5;
public class BasicTradingService {
public BasicTradingService( ) {}
public int getTotalVolume(String[] stocks) {
// get the volumes for each stock from some
// data feed and return the total
int total = 345000;
return total;
}
}
The BasicTradingService class contains the method getTotalVolume( ), which returns the total number of
shares traded. Since we're not going to access a data feed, we'll just return
a made-up value. The method returns an integer and takes a single parameter
called stocks that is an array of String values. The strings in the array are the stock
symbols; in a real application, we'd retrieve the volume for each stock from
our data feed and return the total for all the stocks in the array.
Apache SOAP has built-in support for arrays, so you don't need
to do anything special on the service side. This also means that there's
nothing new in the deployment descriptor; it's built just like it was in the
previous chapter. Here is the deployment descriptor for the urn:BasicTradingService service: <isd:service
xmlns:isd="http://xml.apache.org/xml-soap/deployment"
id="urn:BasicTradingService">
<isd:provider type="java"
scope="Application"
methods="getTotalVolume">
<isd:java class="javasoap.book.ch5.BasicTradingService"
static="false"/>
</isd:provider>
<isd:faultListener>org.apache.soap.server.DOMFaultListener
</isd:faultListener>
<isd:mappings>
</isd:mappings>
</isd:service>
You can deploy the service using this deployment descriptor, or
you can use the Apache SOAP Admin tool from your browser.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|