April 14, 2011

Auto-generate Client Code with WSDL2Java tool of Apache Axis 1.4

0) We shall create a Web Service client for the web service created in this post by auto-generating code with the WSDL2Java tool of Apache Axis 1.4

Copy the WSDL file from the URL into a text file with the extension *.wsdl and save it in a folder on your hard disk (outside the web application structure), for example, C:\my-wsdl-files

1) Repeat step 3 from the following post - http://www.javaissues.com/2011/04/deploying-wsdd-web-services-with-apache.html

2) Open the command prompt (Start > Run):

- change directory to the C:\my-wsdl-files folder (the folder which contains the saved wsdl file/s)

- issue the following command at the command prompt:
java org.apache.axis.wsdl.WSDL2Java HelloWorld.wsdl

Note: Remove the <?xml ... ?> processing instruction lines from the top of the wsdl file, if present.

3) This will generate the client code in the package: localhost.WebAppName.services.HelloWorld
Copy all the 4 classes into a Java project / application (retain the package name)

4) Create a new class - WSClientTest.java - in the same Java project / application (in which the auto-generated code was added) as follows,


package info.icontraining.wsclient;


import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import localhost.WebAppName.services.HelloWorld.*;


public class WSClientTest
{
    public static void main (String args[]) 
         throws ServiceException, RemoteException {
       
         HelloWorldService service = new HelloWorldServiceLocator(); 
         HelloWorld port = service.getHelloWorld(); 
         System.out.println(port.sayHello());
    }
}


5) Execute the WSClientTest class by running it as a standalone Java Application

No comments:

Post a Comment