Web ServicesAccelerate Your Java Web Service Development with XFire

Accelerate Your Java Web Service Development with XFire

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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

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:

<?xml version="1.0" encoding="UTF-8"?>
<project default="compile" name="xfireclient">

   <property environment="env"/>

   <property name="wsdlurl"
      value="http://www.webservicex.net/stockquote.asmx?WSDL" />

   <path id="compile.classpath">
      <fileset dir="${basedir}/lib" includes="**/*.jar" />
   </path>

   <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"
            classpathref="compile.classpath"/>

   <target name="wsgen">
      <wsgen outputDirectory="${basedir}/src"
             wsdl="${wsdlurl}"
             package="net.xmethods.services.stockquote"
             overwrite="true" />
   </target>

   <!-- Clean class files -->
   <target name="clean">
      <delete>
         <fileset dir="bin" includes="**/*.class"/>
      </delete>
   </target>

   <!-- Compiles source files -->
   <target name="compile" depends="clean, wsgen">
      <javac srcdir="src" destdir="bin" debug="on" verbose="off">
         <classpath>
            <fileset dir="lib" />
         </classpath>
      </javac>
   </target>
</project>

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’:

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:

<StockQuotes>
   <Stock>
      <Symbol>HOT</Symbol>
      <Last>67.07</Last>
      <Date>6/29/2007</Date>
      <Time>4:00pm</Time>
      <Change>-1.13</Change>
      <Open>68.04</Open>
      <High>68.77</High>
      <Low>66.035</Low>
      <Volume>3752226</Volume>
      <MktCap>14.427B</MktCap>
      <PreviousClose>68.20</PreviousClose>
      <PercentageChange>-1.66%</PercentageChange>
      <AnnRange>49.68 - 74.35</AnnRange>
      <Earns>5.237</Earns>
      <P-E>13.02</P-E>
      <Name>STARWOOD HOTELS&R</Name>
   </Stock>
</StockQuotes>

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

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.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories