SOAP Web Services have been around for a number of years now. For many years, the two main Java-based SOAP frameworks were Apache Axis and Sun's JAX-WS. A new Java-based SOAP framework by Codehaus called XFire now provides an even simpler way to develop SOAP web services and clients. The first article in this series will create a simple SOAP client using XFire, demonstrating how simple it is to work with this Java SOAP framework. The second article will use XFire to create a web service implementation.
XFire: SOAP Web Services Simplified
XFire is a next-generation Java SOAP framework. It makes service-oriented development approachable through its easy-to-use API and support for standards. It is also highly performant because it is built on a low-memory, StAX-based model. XFire can be used to create SOAP web service implementations and SOAP web service clients. Some important features of XFire are:
- Support for important Web Service standards—SOAP, WSDL, WS-I Basic Profile, WS-Addressing, WS-Security, and so forth.
- High performance SOAP Stack
- Pluggable bindings POJOs, XMLBeans, JAXB 1.1, JAXB 2.0, and Castor support
- JSR 181 API to configure services via Java 5 and 1.4 (Commons attributes JSR 181 syntax)
- Support for many different transports—HTTP, JMS, XMPP, In-JVM, and so on.
- Embeddable and Intuitive API
- Spring, Pico, Plexus, and Loom support.
- JBI Support
- Client and server stub generation
- JAX-WS early access support
Downloading XFire
The first thing you will want to do is get the latest XFire distribution, which is XFire 1.2.6. You can download the xfire-distribution-1.2.6.zip file from the XFire Download page. Once you have downloaded it, you can unzip it to a directory of your choice. I extract it to my C: drive, creating a directory C:xfire-1.2.6.
Downloading Java
For this example, I will use Java SE 5, specifically version 1.5.0_06. You can download the latest Java SE version from the Sun Java Download page. You can follow the installation instructions and install Java SE on to your machine. You also will want to set a JAVA_HOME environment variable as well as add the JAVA_HOMEbin directory to your PATH, so you can run the 'javac' and 'java' commands.
Downloading Ant
To compile and build your Java code, you will use the Apache Ant build tool. You can download the latest version of Ant, version 1.7.0 from the Ant Download page. All that needs to be done is unzip the file to a directory. I extract it to my C: drive, creating a directory C:ant-1.7.0. You also will need to set an ANT_HOME environment variable as well as add the ANT_HOMEbin to your PATH, so you can run the 'ant' command. For this example, I will be using an older version, 1.6.5.
Downloading Eclipse
For this example, I will use the Eclipse IDE to do my development. I am currently running Eclipse version 3.2.2. You can download the latest version of Eclipse from the Eclipse Download page and follow the installation instructions and install Eclipse on to your machine.
Creating Your XFireDemo Project in Eclipse
Create an XFireDemo Java project in Eclipse. Within this project, create the 'bin', 'src', 'lib' subdirectories, as well as a 'build.xml' file. Copy the C:xfire-1.2.6xfire-all-1.2.6.jar file and C:xfire-1.2.6lib*.jar files to the XFireDemo project 'lib' directory. Once you are done, your project should look like this:
Figure 1:XFireDemo Eclipse Project
Filling in the Ant build.xml File
You are going to use Ant to build your XFire test, so you need to create your build.xml file. The build file contains three targets: one to clean the build, called 'clean'; one to generate our SOAP client Java classes from the web service WSDL definition, called 'wsgen'; and one to compile the project, called 'compile'. Here is the build.xml file with the 'clean' and 'compile' targets filled in:
You will compile your Java source files from the 'src' directory, creating Java classes in the 'bin' directory.
XMethods Stock Quote Web Service
You are going to create a SOAP client for the XMethods Stock Quote web service. This simple SOAP web service takes a String parameter representing a stock company symbol and returns the stock quote for this symbol. This web service has a WSDL located at the URL http://www.webservicex.net/stockquote.asmx. You will use XFire to generate SOAP client Java classes that you can use to access this web service.
Generating SOAP Client Java Classes from a WSDL Using XFire
XFire provides an Ant task that allows a user to generate the Java classes for the SOAP client of a web service. It does this by inspecting a WSDL definition for that web service. You are going to point XFire to the XMethods Stock Quote WSDL URL and have it generate your SOAP client Java classes for you. To do this, you need to set up your 'wsgen' Ant task in our build.xml file. Add the following to the 'build.xml' file:
You define a 'wsdlurl' property that contains your StockQuote web service WSDL URL. You also specify your Java compilation classpath by using a path variable 'compile.classpath'. You define the XFire built-in 'wsgen' Ant task definition. You then specify your 'wsgen' ant target that uses the XFire 'wsgen' taskdef, pointing to the 'wsdlurl', to generate your StockQuote web service SOAP client Java classes in the 'net.xmethods.services.stockquote' package. When you run this task, you will see the following new Java classes in your project:
Figure 2:StockQuote SOAP web service client Java classes generated by XFire
Coding the StockQuote Web Service Client
You create a Java class file called XFireClientTest in the 'src' directory. The 'src' directory should now look like this:
Figure 3:XFireClientTest Java class
Fill in the XFireClientTest.java file with the following Java code:
import net.xmethods.services.stockquote.StockQuoteClient;
import net.xmethods.services.stockquote.StockQuoteSoap;
public class XFireClientTest
{
public static void main(String[] args) {
StockQuoteClient client = new StockQuoteClient();
StockQuoteSoap service = client.getStockQuoteSoap();
String quote = service.getQuote("HOT");
System.out.println(quote);
}
}
Your client gets the client proxy for the StockQuote service. It then uses it to get the StockQuote service and call the 'getQuote()' service method, passing in the company symbol of 'HOT'. It then prints out the result to the console. That is all there is for you to code; XFire has generated all the other marshalling code that will convert your input parameter into a SOAP request.
Compiling the XFireDemo Project
Now that you have your Stock Quote web service client, you can go ahead and compile your project using Ant. Running 'ant build.xml' produces the following output, indicating that it compiled your source code and created your XFireClientTest and supporting class files in the 'bin' directory:
Buildfile: C:Documents and SettingsdominicworkspaceXFireDemo
build.xml
clean:
[delete] Deleting 8 files from C:Documents and Settingsdominic
workspaceXFireDemobin
wsgen:
[wsgen] Jun 30, 2007 5:02:46 PM
org.codehaus.xfire.gen.Wsdl11Generator generate
[wsgen] INFO: Generating code for WSDL at
http://www.webservicex.net/stockquote.asmx?WSDL with a
base URI of http://www.webservicex.net/stockquote.asmx?WSDL
[wsgen] Jun 30, 2007 5:02:51 PM
org.codehaus.XFire.gen.jsr181.AbstractServiceGenerator
generate
[wsgen] INFO: Creating class
net.xmethods.services.stockquote.StockQuoteSoap
[wsgen] Jun 30, 2007 5:02:51 PM
org.codehaus.xfire.gen.jsr181.AbstractServiceGenerator
generate
[wsgen] INFO: Creating class
net.xmethods.services.stockquote.StockQuoteImpl
[wsgen] netwebservicexGetQuote.java
[wsgen] netwebservicexGetQuoteResponse.java
[wsgen] netwebservicexObjectFactory.java
[wsgen] netwebservicexpackage-info.java
[wsgen] netxmethodsservicesstockquoteStockQuoteClient.java
[wsgen] netxmethodsservicesstockquoteStockQuoteImpl.java
[wsgen] netxmethodsservicesstockquoteStockQuoteSoap.java
compile:
[javac] Compiling 8 source files to C:Documents and Settings
dominicworkspaceXFireDemobin
[javac] Note: C:Documents and Settingsdominicworkspace
XFireDemosrcnetxmethodsservicesstockquote
StockQuoteClient.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
BUILD SUCCESSFUL
Total time: 14 seconds
Running the StockQuote Service SOAP Client Test
You can run your client directly from Eclipse by right-clicking the XFireClientTest class and selecting 'Run As->Java Application':
Click here for a larger image.
Figure 4:Running XFireClientTest in Eclipse
When you run the SOAP client test class, XFireClientTest, using a company symbol of 'HOT' (Starwood Hotels and Resorts), you will get the following response from the Stock Quote web service, indicating a successful test:
HOT
67.07
6/29/2007
-1.13
68.04
68.77
66.035
3752226
14.427B
68.20
-1.66%
49.68 - 74.35
5.237
13.02
STARWOOD HOTELS&R
Conclusion
This article took a look at XFire, a Java SOAP framework that makes working with web services fast and simple. You created a SOAP client and saw how easy it is to use XFire. In the next article in this XFire series, you will create your own SOAP web service using XFire.
References
- Codehaus XFire: http://xfire.codehaus.org/
- Sun Java: http://java.sun.com/
- Apache Ant: http://ant.apache.org/
- Eclipse: http://www.eclipse.org/
- XMethods: http://www.xmethods.net/
- SilvaSoft, Inc. weblog: http://jroller.com/page/silvasoftinc
About the Author
Dominic Da Silva (http://www.dominicdasilva.com/) is the President of SilvaSoft, Inc., a software consulting company specializing in Java- and Ruby-based web and web services development. He has worked with Java since the year 2000 and is a Linux user from the 1.0 days. He is also Sun Certified for the Java 2 platform. Born on the beautiful Caribbean island of Trinidad and Tobago, he now makes his home in sunny Orlando, Florida.
Tidak ada komentar:
Posting Komentar