Creating the client code
Now it’s time to set up a proper client. We’ll reuse the Swing client from "
Struts meets Swing
(2)". A few words on this client--it issues the requests through a helper
class called WebRequest. Its interface is very much oriented towards the
URLConnection class that is used to talk to the Struts servlet, so we
cannot use it as it is. We’ll therefore create a new helper class called
SOAPRequest, with methods corresponding to the service calls we want to
issue, and then change the Swing client code correspondingly.
Here’s a picture of the architecture we want to achieve:

The methods
in SOAPRequest are these--note that for simplicity I’ve given them a
signature identical to the methods in the DVDService class:
|
Action |
SOAPRequest method |
|
return DVD titles |
Vector getTitles() |
|
return DVD details |
DVD getDetails(int i) |
|
update DVD |
void update(DVD dvd) |
|
create DVD |
void create(DVD dvd) |
|
delete DVD |
void delete() |
|
save DVDs |
void save() |
The general layout of a client that talks SOAP is this:
- create a Call object (for creating a dynamic RPC call)
- set the URL of the web service
- define serializers/deserializers for classes
- set the name of the method to invoke
- set type and data for the input parameters
- set the type of the result parameter
- issue the call
- handle the result
Since we’re transmitting the DVD JavaBean as part of three of our
requests we’ll need a serializer and a deserializer for this class. Luckily Axis
has a serializer that we can use. You remember that we defined the DVD
bean like this in the deployment file:
<beanMapping qname="myNS:DVD" xmlns:myNS="urn:BeanService"
languageSpecificType="java:hansen.playground.DVD"/> |
This gives the bean a QName of [namespace]:[localname]
=[urn:Beanservice]:[DVD].
As an example I’ll now sketch the Java code for calling the getDetails
service. The numbers in the comments correspond to the numbered list above:
int index = 1; // DVD number 2
// Create JAX-RPC setup
Service service = new Service();
Call call = (Call) service.createCall(); // *1*
String endpoint =
"http://localhost:8080/axis/services/DVDService";
call.setTargetEndpointAddress(
new java.net.URL(endpoint) ); // *2*
// Define the DVD bean
QName qn = new QName( "urn:BeanService", "DVD" );
call.registerTypeMapping(DVD.class, qn, // *3*
new org.apache.axis.encoding.ser.BeanSerializerFactory
(DVD.class, qn),
new org.apache.axis.encoding.ser.BeanDeserializerFactory
(DVD.class, qn));
call.setOperationName( "getDetails" ); // *4*
call.addParameter( "arg1",
XMLType.XSD_INT, ParameterMode.IN); // *5*
call.setReturnType( qn ); // *6*
DVD dvd = (DVD)call.invoke(
new Object [] { new Integer(index) }); // *7*
System.out.println("Results : ");
System.out.println(dvd.getId() + "/" + // *8*
dvd.getTitle() + "/" +
dvd.getLength() + "/" +
dvd.getActors()); |
Figure 4: A typical client web service call
In our SOAPRequest helper class we’ll only execute the statements 1-3
once. For each method in SOAPRequest we must therefore write code
matching statements 4-8.
Since we reuse the Call object instance every method starts by
deleting the previous definition of parameters:
call.removeAllParameters();
Click to see the
complete code for the SOAPRequest class.
In SOAPRequest I’ve coded a main method that tests all the methods.
For demonstration purposes several messages are written to System.out,
and this is also the case for the DVDService class. So, it’s easy to see
if things work the way they were intended to work.
To run the main method you enter these commands in your DOS-window (assuming
you’re located in the WEB-INF directory):
setcp
cd classes
java hansen.playground.SOAPRequest
It’s now a simple matter to make some modifications to the Swing client in
order for it to use the SOAPRequest class in stead of the
WebRequest class. Click to see the
complete code for the Swing DVDClient .
To start the client enter:
java hansen.playground.DVDClient
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.