Creating a Webservice client with XFire

As promised before (in this post) here is how to simply create a webservice client with XFire. I assume you have built a web service and have it running in your JBoss installation. First of all I have created this directory structure:
-XFireClientTest
  -build
  -lib
  -src
    -main
      -java
    -test
      -java

In the lib directory I have placed the following jar files:

commons-codec-1.3.jar
commons-httpclient-3.0.1.jar
commons-logging-1.0.4.jar
j2ee.jar
jaxb-api-2.0.jar
jaxb-impl-2.0.1.jar
jaxb-xjc-2.0.1.jar
jdom-1.0.jar
stax-api-1.0.1.jar
test.txt
wsdl4j-1.5.2.jar
wstx-asl-2.9.3.jar
xfire-all-1.1.2.jar
xfire-jsr181-api-1.0-M1.jar
XmlSchema-1.0.3.jar

The next piece of code is the Ant build file I used to generate the webservice client code. This file is placed in the root directory of the project ‘XFireClientTest’. Here it is:

<?xml version="1.0"?>
<project name="XFireClientTest" default="generate-client" basedir=".">

	<description>
		Ant build script for the XFireCLientTest module
	</description>

	<!-- Directory-specifications -->
	<property name="compile.debug" value="true" />
	<property name="lib.dir" value="${basedir}/lib" />
	<property name="build.dir" value="${basedir}/build" />
	<property name="src.dir" value="${basedir}/src/main/java" />
	<property name="src-test.dir" value="${basedir}/src/test/java" />

	<!-- == CLASSPATH definitions == -->
	<path id="build.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<path id="test.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		</fileset>
		<pathelement location="${build.dir}" />
	</path>

	<!-- === preparations === -->
	<target name="prepare">
		<mkdir dir="${build.dir}" />
	</target>

	<!-- == cleans the module == -->
	<target name="clean">
		<delete dir="${build.dir}" />
	</target>

	<!-- === compiles the module === -->
	<target name="compile" depends="prepare">
		<javac sourcepath="" srcdir="${src.dir}" destdir="${build.dir}" debug="${compile.debug}">
			<classpath refid="build.classpath" />
		</javac>
		<javac sourcepath="" srcdir="${src-test.dir}" destdir="${build.dir}" debug="${compile.debug}">
			<classpath refid="build.classpath" />
		</javac>
	</target>


	<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="build.classpath" />
	<target name="generate-client">
		<wsgen outputDirectory="${basedir}/src/main/java" wsdl="http://localhost:8080/XFireTest/services/BookService?wsdl" package="net.pascalalma.client" overwrite="true" />
	</target>
</project>

Of course you may want to change the location of your wsdl file you want to use. When all this is in place you can now generate your client by running the Ant target ‘generate-client’. It will then create various Java classes in the src/main/java directory. To run the generated client for my wsdl I have created a class ‘BookServiceClientTest’ and placed it in the src/test/java directory. Here it is:

package net.pascalalma.tests;

import java.util.List;

import net.pascalalma.client.BookServiceClient;
import net.pascalalma.client.BookServiceImpl;
import net.pascalalma.model.ArrayOfBookObject;
import net.pascalalma.model.BookObject;

public class BookServiceClientTest {

   public static void main(String[] args) {
      BookServiceClient client = new BookServiceClient();
      BookServiceImpl service = client.getBookServiceHttpPort();

      ArrayOfBookObject booksArray = service.getBooks();
      List books = booksArray.getBookObject();

      for (BookObject book: books) {
         System.out.println(book.getTitle().getValue());
      }
    }
}

When you run this class the webservice will be invoked and the returned books are printed out. As you can see it is very easy to create a webservice client based on the wsdl with XFire.

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in Web Service and tagged . Bookmark the permalink.