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:

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


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=".">

		Ant build script for the XFireCLientTest module

	<!-- 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" />

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

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

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

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

	<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" />

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) {

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 software developer and architect. Pascal has been designing and building applications since 2001. He is particularly interested in Open Source toolstack (Mule, Spring Framework, JBoss) and technologies like Web Services, SOA and Cloud technologies. Lately he is having great fun by building iOS apps with Swift. Specialties: JEE AWS XML/XSD/XSLT Web Services/SOA Mule ESB/ WSO2 ESB Maven Cloud Technology Swift/ iOS
This entry was posted in Web Service and tagged . Bookmark the permalink.