Introduction To Web Services - Implementing A Java Web Service
Introduction To Web Services - Implementing A Java Web Service
Page 1 of 16
Contents
Contents ................................................................................ 2
Introduction........................................................................... 3
About this tutorial
3
Web service - a programmatic definition
3
Installing software
4
Implementing a Simple Web Service ................................... 5
Implementing a Java Web Service Client............................ 9
Implementing a Microsoft .NET Web service client..........12
1.6. SOAP messages at glance ..........................................13
Introduction
13
SOAP message structure
13
Cleanup and Review ...........................................................15
Review of What We Learned
15
Copyright and Disclaimer ...................................................16
Page 2 of 16
Introduction
Page 3 of 16
Installing software
REQUIREMENTS: We assume that you have a Java 1.4 or later SDK and a
standard HTTP browser installed on your system and the JAVA_HOME
system variable points to the Java root directory.
If you want to follow along with the demo, you'll need to download WASP
Server for Java, 4.5 (
) from the
http://www.systinet.com/products/wasp_jserver/overview
Systinet Web site. Unpack the downloaded package to a local disk and run
the install script from the bin subdirectory of the WASP Server installation.
You'll need to agree to the product license and then please answer with
default options to all installation questions (simply press Enter multiple
times).
IMPORTANT: Please set the WASP_HOME environment variable to point to
the WASP Server root directory.
You'll also need to download the tutorial sources
(http://dev.systinet.com/get&dl=dHV0b3JpYWxzLnppcA) and unpack it on
the local disk.
Page 4 of 16
We'll perform the following steps to create our simple Web service:
Page 5 of 16
/*
* StockQuoteService.java
*
* Created on Sat 13th Oct 2001, 15:25
*/
package com.systinet.demos.stock;
/**
* Simple stock quote service
*
* @version 1.0
*/
public class StockQuoteService {
Our example is yet another simple stock quote system (we've seen so
many of these, developers should be registered traders by now), but it
illustrates how easily Web services can be created and deployed. In our
example, we're going to retrieve the price of three stocks (BEAS, MSFT,
and SUNW).
The easiest way to turn our class into a Web service is to compile our Java
classes and then use the deployment tool to deploy them to the SOAP
server.
Page 6 of 16
NOTE: You'll find all scripts in the bin subdirectory of the unpacked tutorial
sources archive.
NOTE: Before running this example you need to install the Systinet Web
services framework, WASP Server for Java. Please see the Installation
chapter of this document for step-by-step installation.
First we start WASP Server with the serverstart.bat script from the WASP
bin directory. Then we compile StockQuoteService.java and deploy the
compiled class to the server using the run.bat deploy_stock command in
the tutorials bin directory from the command-line console.
Next we'll make sure that everything worked properly by opening the
administration console (if you accepted the default install,
http://localhost:6060/admin/console) in the HTTP browser. Go to the Web
services tab in the admin console and click on the StockQuoteService_inst
service from the List of service instances table. Then click on the URL link
in the List of service instance's endpoints and you should see the Web
service WSDL file in the browser pop-up window. Notice that WASP Server
automatically generated the WSDL file and made it publicly available at
http://localhost:6060/StockQuoteService/.
The WSDL file contains a full description of the deployed Web service.
Basically there are three parts in a WSDL file:
Page 7 of 16
As you can see, a WSDL file completely describes a Web service. Given this
WSDL file, we have all the information we need to create a client
application that can access our stock quote Web service.
Page 8 of 16
A Java client binds to a remote Web service using a proxy component. This
proxy component can be statically generated from the WSDL file at
development time or dynamically generated at runtime. In this example
we'll use a dynamically generated proxy. We need a Java interface that can
keep a reference to this dynamically created object. We can either create
the interface ourselves, or we can use WASP's WSDL2Java compiler to
generate one for us. The interface creation is easy since the only
requirement is that the interface methods must be a subset of methods of
the Web service business logic Java class. The code below shows the Web
service interface as generated by the WSDL2javab tool.
/**
*/
double getQuote(java.lang.String p0);
/**
*/
java.util.LinkedList getAvailableStocks();
}
/*
* Generated by WSDLCompiler, (c) 2001, Systinet, Inc.
*
http://www.systinet.com
*/
Now let's look at the client code in Figure 3. All the client needs to do is
lookup the Web service and bind to its proxy. This is handled through
the WASP Web service Registry by invoking its lookup method. The
lookup method requires two parameters: a reference to a WSDL file and
the class of the Java interface that will reference the proxy instance. The
lookup method returns the proxy that is used to invoke the Web service.
Each time the client invokes an operation on the proxy, the request is
automatically translated into a SOAP request and sent to the Web
service.
Page 9 of 16
/**
* Stock Client
*
* @version 1.1
*/
package com.systinet.demos.stock.client;
import org.systinet.wasp.webservice.Registry;
import java.net.InetAddress;
public class StockQuoteClient {
/**
* Web service client main method.
* Finds the web service and
* @param args not used.
*/
public static void main( String[] args ) throws Exception
{
// obtain real localhost (required by Soap Spy)
String localhost =
InetAddress.getLocalHost().getHostName();
// lookup and bind to StockQuoteService
StockQuoteService service =
(StockQuoteService)Registry.lookup(
"http://"+localhost+":6060/StockQuoteService/",
StockQuoteService.class
);
// use StockQuoteService
System.out.println("Getting available stocks");
System.out.println("------------------------");
java.util.LinkedList list =
service.getAvailableStocks();
java.util.Iterator iter = list.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
System.out.println("");
System.out.println("Getting SUNW quote");
System.out.println("------------------------");
System.out.println("SUNW "+service.getQuote("SUNW"));
System.out.println("");
}
}
Page 10 of 16
Run the run.bat make_stock command from the bin directory. This script
will invoke the WSDL2Java tool and generate the Java interface, and then
it will compile the client application. After that you can run the run.bat
run_stock command. You should see the output from the
getAvailableStocks and getQuote methods on the console.
WASP Server supports other APIs that you can use to invoke Web services
including JAXM and JAX-RPC. Please see the jaxm or jaxrpc demos from
the WASP Server distribution for comprehensive code examples.
Page 11 of 16
Implementing a Microsoft
.NET Web service client
Page 12 of 16
Introduction
Now we can use the WASP SOAP Spy tool to view the SOAP messages that
are exchanged between client and service. First, we need to start SOAP
Spy by invoking the SoapSpy.bat script from the bin subdirectory of the
WASP Server installation. Start the SOAP Spy listener by clicking on the
left-most icon in the toolbar. Then run the Java Web service client using
the run.bat spy_stock command and SOAP Spy should display the
exchanged SOAP messages. Modify one of the request messages and click
the resend button. Check out the new response.
<ENVELOPE attrs>
<HEADER attrs>
<directives/>
</HEADER>
<BODY attrs>
<payload/>
or
<FAULT attrs>
<errors/>
</FAULT>
</BODY>
</ENVELOPE>
Page 13 of 16
requirements beyond standard XML rules, while the RPC style defines rules
for marking up the method call with all its parameters. The SOAP
specification recommends but doesn't mandate an encoding style, the basic
framework for expressing typed values in a SOAP message. The SOAP
encoding style is based on the data types defined in the XML Schema, Part
2 Recommendation which includes primitive programming language types
such as int, float, double, or string. SOAP Encoding also defines rules for
building complex types (e.g. arrays, structures, etc.) on top of these
primitives. In our case (we are using the RPC style with SOAP encoding),
the Body section of the input message contains the invoked method name
with encoded parameters:
<n0:getQuote
xmlns:n0="http://systinet.com/wsdl/com/systinet/demos/stock/
StockQuoteService">
<p0 i:type="d:string">SUNW</p0>
</n0:getQuote>
The output message contains the result of the method call:
<n0:getQuoteResponse
xmlns:n0="http://systinet.com/wsdl/com/systinet/demos/stock/
StockQuoteService">
<response i:type="d:double">10.0</response>
</n0:getQuoteResponse>
A SOAP message can also contain a HEADER section, which is usually used
to propagate context information, such as payment details, transaction
context, or security credentials. We'll show the use of Header sections in
subsequent articles. If an error occurs during processing, the SOAP server
should return a FAULT in the Body section. The Fault element should
contain a fault code, a fault description, and any details available about the
fault. We'll show SOAP error processing in the next article.
Page 14 of 16
Now that we're finished, we should undeploy our Web service from the
server by running the run.bat clean script.
Page 15 of 16
This document and the information contained herein are the property of
Systinet Corporation and shall not be reproduced or copied in whole or in
part without written permission of Systinet Corp.
Copyright 2003 Systinet Corp. All Rights Reserved.
The information in this document is preliminary and is subject to change
without notice and should not be construed as a commitment by Systinet
Corporation.
SYSTINET CORPORATION SHALL HAVE NO LIABILITY FOR THIS
DOCUMENT, INCLUDING ANY LIABILITY FOR NEGLIGENCE. SYSTINET
CORPORATION MAKES NO WARRANTIES, EXPRESS, IMPLIED, STATUTORY,
OR IN ANY OTHER COMMUNICATION. SYSTINET CORPORATION
SPECIFICALLY DISCLAIMS ANY WARRANTY OF MERCHANTABILITY OR
SATISFACTORY QUALITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
AND NON-INFRINGEMENT.
Systinet, WASP, "The Web Services Infrastructure Company" and "Web
Services That Work" are trademarks of Systinet Corp.
Java and all Java-based marks are trademarks or registered trademarks of
Sun Microsystems, Inc. in the U.S. and other countries.
Microsoft, Windows and Windows NT and the Windows logo are trademarks
or registered trademarks of Microsoft Corporation in the United States and
other countries.
UNIX is a registered trademark of The Open Group in the United States and
other countries.
Other company, product, and service names mentioned in these
documents may be trademarks or service marks of others.
Systinet Corp.
Five Cambridge Center, 8th Floor
Cambridge, MA 02142
Phone: 1.617.868.2224
www.systinet.com
Page 16 of 16