As promised before (in this blog ) 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <?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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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<bookObject> 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.
Pingback: Pascal’s Blog » Using SoapUI for testing your webservice
Pingback: Using SoapUI for testing your webservice | Redstream Blog
Pingback: Using SoapUI for testing your webservice | Redstream Blog