Wednesday, September 17, 2008

Apache Axis2 java2wsdl Usage - Graphical Tool

It seems that quite a few people are having trouble using the java2wsdl tool that comes with Axis2. Using a command line tool can be tricky for some users... but wait there is a graphical tool for this too. WSO2 WSAS (Web Services Application Server) has a graphical tool that wraps the java2wsdl utility in Axis2. You can either try it out by downloading WSAS (WSO2 WSAS is open source and available under Apache License v2.0) or if you are in a hurry you could try the java2wsdl graphical tool online. This tool takes you through a wizard like approach and is much easier to use (The tool hosted online is based on WSO2 WSAS 2.3 which is in turn based on Axis2 1.3 with a few bug fixes). You could fine a lot more tools that could help you at http://wso2.org/tools.

For the geeks who do not want to use the graphical tool this is how the command line utility can be used. Simple executing the java2wsdl script without any arguments would show you its usage,

keith@keith:/opt/axis2-1.4.1/bin$ sh java2wsdl.sh
Using AXIS2_HOME: /opt/axis2-1.4.1
Using JAVA_HOME: /opt/software/java/jdk1.5.0_06
Usage: java2wsdl [options] -cn <fully qualified class name>

where [options] include:
-o <output location> output directory
-of <output file name> output file name for the WSDL
-sn <service name> service name
-l <soap address> address of the port for the WSDL
-cp <class path uri> list of classpath entries - (urls)
-tn <target namespace> target namespace for service
-tp <target namespace prefix> target namespace prefix for service
-stn <schema target namespace> target namespace for schema
-stp <schema target namespace prefix> target namespace prefix for schema
-st <binding style> style for the WSDL
-u <binding use> use for the WSDL
-nsg <class name> fully qualified name of a class that implements NamespaceGenerator
-sg <class name> fully qualified name of a class that implements SchemaGenerator
-p2n [<java package>,<namespace] [<java package>,<namespace]...
java package to namespace mapping for argument and return types
-p2n [all, <namespace>] to assign all types to a single namespace
-efd <qualified/unqualified> setting for elementFormDefault (defaults to qualified)
-afd <qualified/unqualified> setting for attributeFormDefault (defaults to qualified)
-xc class1 -xc class2... extra class(es) for which schematype must be generated.
-wv <1.1/2.0> wsdl version - defaults to 1.1 if not specified
-dlb generate schemas conforming to doc/lit/bare style

Most often that not you wont be using most of these options. Assuming that your,
output directory is /home/keith/projects/axis2_rest/resources/
output Filename is StudentService.wsdl
service name is StudentService
classes are in the directory /home/keith/projects/axis2_rest/classes
service implementation class is prg.apache.axis2.StudentService
you need WSDL 2.0

your usage of java2wsdl would be,

sh java2wsdl.sh -wv 2.0 -o /home/keith/projects/axis2_rest/resources/ -of StudentService.wsdl -sn StudentService -cp /home/keith/projects/axis2_rest/classes/ -cn org.apache.axis2.StudentService

3 comments:

Tihamer said...

Thanks for the hints!

However, no, sh did not work for me at all. But the following *DID* work.

C:\sourcelongpath\web>java2wsdl -wv 2.0 -o C:\sourcelongpath\web -of ttfDemo.wsdl -sn ttfDemo -cp C:\compiled\classes\ -cn org.fully.qualified.ttfDemo
Using AXIS2_HOME: C:\Program Files\Apache Software Foundation\axis2-1.5
Using JAVA_HOME: C:\Program Files\Java\jdk1.6.0_13

where -wv is the Wsdl version
-o is the output directory where the wsdl file will be put
-of is the output file name (ttfDemo.wsdl in my case)
-sn is the service name
-cp is the class path (which leads up to the *root* of your compiled class directory)
-cn is the fully qualified class name; i.e. the package name (first line of your java file) dot your class name.

Note for newbies and people who glommed onto Eclipse without really understanding CLASSPATH, make sure that the classpath points to the *root* of the class directory, and not all the way to the class itself. The following did not work because of this classpath mistake:

C:\sourcelongpath\web>java2wsdl -wv 2.0 -o C:\sourcelongpath\web -of ttfDemo.wsdl -sn ttfDemo -cp C:\compiled\classes\org\fully\qualified\ -cn org.fully.qualified.ttfDemo
Using AXIS2_HOME: C:\Program Files\Apache Software Foundation\axis2-1.5
Using JAVA_HOME: C:\Program Files\Java\jdk1.6.0_13
Exception in thread "main" java.lang.ClassNotFoundException: org.fully.qualified.ttfDemo
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
{snip}
at org.apache.ws.java2wsdl.Java2WSDL.main(Java2WSDL.java:32)

Unknown said...

Tihamer: good observation. I'm using Eclipse on day to day basis and couldn't figure it out until read your comment :-)

Thanks.

Rem

Unknown said...

Hi,
Do you know if there is a way to define the attribute "name" of the response element?

I mean, I generated a WSDL and my reponse element is:








but I need it like this:










Can you help me?

Thanks.