The wsdl2java tool generates the stub code from the WSDL contract. The stub code provides the supporting code that is required to invoke operations on the remote service.
For consumers, the wsdl2java tool generates the following types of code:
Stub code — Supporting files for implementing a consumer.
Starting point code — Sample code that connects to the remote service and invokes every operation on the remote service.
Ant build file — A build.xml
file intended for use with the Ant build utility. It has targets for
building and for running the sample consumer.
To generate consumer code use the wsdl2java tool. Enter the following command at a command-line prompt:
wsdl2java -ant -client -d outputDir
hello_world.wsdl
Where outputDir
is the location of a directory where the generated files are placed and
hello_world.wsdl
is a file containing the contract shown in Example 3.1.
The -ant
option generates an ant build.xml
file, for use with the ant build utility. The
-client
option generates starting point code for the consumer's main()
method.
For a complete list of the arguments available for the wsdl2java tool see
wsdl2java in
If you are using Apache Ant as your build system, you can call the code generator using Ant's java task, as shown in Example 5.1.
Example 5.1. Generating Service Starting Point Code from Ant
<project name="myProject" basedir="."> <property name="fsf.home" location ="InstallDir
"/> <path id="fsf.classpath"> <fileset dir="${fsf.home}/lib"> <include name="*.jar"/> </fileset> </path> <target name="ServiceGen"> <java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true"> <arg value="-ant"/> <arg value="-client"/> <arg value="-d"/> <arg value="outputDir
"/> <arg value="myService.wsdl
"/> <classpath> <path refid="fsf.classpath"/> </classpath> </java> ... </target> ... </project>
The command line options are passed to the code generator using the task's arg
element. Arguments that
require two strings, such as -d
, must be split into two arg
elements.
The preceding command generates the following Java packages:
org.apache.hello_world_soap_http — This package is generated from the
http://apache.org/hello_world_soap_http
target namespace. All of the WSDL entities defined in this
namespace (for example, the Greeter port type and the SOAPService service) map to Java classes this Java package.
org.apache.hello_world_soap_http.types — This package is generated from the
http://apache.org/hello_world_soap_http/types
target namespace. All of the XML types defined in
this namespace (that is, everything defined in the wsdl:types
element of the HelloWorld contract) map
to Java classes in this Java package.
The stub files generated by the wsdl2java tool fall into the following categories:
Classes representing WSDL entities in the org.apache.hello_world_soap_http package. The following classes are generated to represent WSDL entities:
Greeter
— A Java interface that represents the Greeter
wsdl:portType
element. In JAX-WS terminology, this Java interface is the service endpoint interface
(SEI).
SOAPService
— A Java service class (extending javax.xml.ws.Service
) that
represents the SOAPService wsdl:service
element.
PingMeFault
— A Java exception class (extending
java.lang.Exception
) that represents the pingMeFault wsdl:fault
element.
Classes representing XML types in the org.objectweb.hello_world_soap_http.types package. In the HelloWorld example, the only generated types are the various wrappers for the request and reply messages. Some of these data types are useful for the asynchronous invocation model.