Wednesday, March 30, 2011

How to convert WSDL to Java

Background


Basically WSDL (Web Services Development Language) is an XML document used to describe or locate web services. WSDL stands for Web Services Description Language. The document describes a Web service. It specifies the location of the service and the operations that can be taken. 

WSDL2Java is distributed with Axis2 package of Apache and need to set-up Axis2 to use this service. A guide to set-up Axis2 can be found in apache organization documents .WSDL 2.0 is the currently using version. Obviously JDK should be properly installed too. Note: For the 3rd method you are not needed to set these things by yourself.

The WSDL document includes server Java programs for Web services and generated classes using WSDL2Java feature includes client stubs, server skeletons and data types that can help in writing client side. The generated stub classes play a great role in distributed computing and if you are interested can read on what are stub classes. In brief talking, if there is an interested web service, that you want to consume, and if you have the WSDL file of service you can generate the stub and code the client to use stub. This will need lot more plug-ins and dependencies to work properly and will have to configure as needed.


Methods

The following are few methods to convert WSDL to java 

  (1)  Using Maven


First create a Maven module in whatever IDE you use (As I know Netbeans and IntelliJ IDEA support this). It will create a project structure as follows. As you can see a default pom.xml file will be created. This is to include relevant details needed to build project in Maven and if you are new to pom.xml files intoduce yourself the pom.xml.
          
There inside the 'main' directory create another directory named 'resources' parallel to java directory. Java directory is to hold the source code relevant to client that you are going to generate. For the moment it can be empty. 


Place the relevant WSDL files of the services you are interested, inside resources as shown above and now lets go for the pom.xml file.





There in the task section, include all the WSDL files that you want to convert to Java as in the above figure and edit the "arg line" so as to identify the relevant WSDL file of each web service. You will have to edit few other sections like plug-ins and resources and those should be done as needed by the web services you are trying to consume.

Once you have finished above steps open a terminal and go to the directory where pom.xml file is located. Then enter the command "mvn clean " and then "mvn install". Of course you can enter "mvn clean install" at once. But separating it is the recommended way as procedure is clear by then.

 If you have set-up Axis2 properly and edited pom.xml correctly this command will generate a directory called 'target' and it will include the relevant stub classes and few other classes needed by the project. Now you can carry on developing the client to consume services in the stub classes.

   ( 2)Using Apache Ant


 Apache-ant is another tool that can be used to generate stub classes. I have not tried out it yet and here is a guidance to build Stubs using ant.

  (3) In Command-line


Converting WSDL to Java can be done using terminal commands too. First create your folder structure and go inside the directory you are going to code the client. Then to generate stub class enter the following command,
"wsdl2java -s -uw -uri URL or path to WSDL file"
Here -s implies web service invocation is synchronous. Simply once the client make a request, it waits without doing anything else until the reply comes.

          -uw implies unwrapping option switch on. This creates the methods in stub classes are capable of one-step unwrapping of the XML data in a message.

         -uri implies URL or path of the WSDL file

This command will create folder structure according to the WSDL file and will create an ant build.xml file. Inside the src directory in some inner directory you will find the generated stub classes. Now the client code can be placed there or somewhere else importing the relevant stub classes.

   (4) Using WSO2 ESB


Here you do not need to have installed Axis2. Instead except for installation of JDK, you need to install WSO2 ESB. This is not a big task and downloading, running the system, accessing service via browser are the steps to follow. You can have detailed instructions at WSO2 Enterprise Service Bus (ESB) Installation Guide . There in the user interface you will find the tool WSDL2Java among few other tools like Java2WSDL in the left-hand side.


 When you go for WSDL2Java tool it will allow you to enter the relevant path of WSDL file and few other options. You can select them as want and click on "Generate" button.



 This will automatically download the generated code with stub classes to your local machine and you can place it any where as needed and carry on developing the client.

I am preferring the first method as we can see what really is happening and directly the procedure is inside the project. There can be smarter methods than these four.