Generating a SOAP Client using WSDL

There are various third-party libraries available for generating a client based on WSDL.

We will examine two of the many available ways of generating a client.

  • Using Axis's WSDL2Java framework

    You can run the WSDL2Java class with the location of the WSDL from the command prompt. Usage: java org.apache.axis.wsdl.WSDL2Java <location of the WSDL> -o <output folder>

    For example, for the HelloWorld service that is created, you can run the following java -classpath C:\views\third-party\axis\axis-1_4\lib\*;C:\views\third-party\axis\axis-1_4\lib org.apache.axis.wsdl.WSDL2Java http://localhost/oms/services/PCTA/HW?wsdl -o src1 which will generate a client for the HelloWorld service under src1 folder

  • Using Eclipse for J2EE application

    In the Enterprise Edition of Eclipse (download-able from http://www.eclipse.org/downloads/), you would create a new Dynamic Web Project and import the WSDL into the project. For this, the WSDL has to be copied from the URL into a file with an extension of .wsdl

    In our example, the WSDL is located at http://localhost/oms/services/PCTA/HW?wsdl. So, you can copy the XML into a file and save it as somefilename.wsdl

    Now, import the WSDL by right-clicking on the Dynamic web project and click on import. Select file system in the pop-up window, click next and in the next window, browse to the location of the wsdl, and click finish. Once the WSDL is imported, right-click on the WSDL, navigate to Web Services, and click on Generate Client as shown below:
    images/download/attachments/144836063/eclipse_generate_client_using_wsdl-version-1-modificationdate-1645136946000-api-v2.png
    A set of java classes and interfaces will be generated like the following:
    To use the web services, create a class and create a new object of the *Proxy.java and get the reference to the interface containing the web service methods. You will use this to call the methods/services available. However, we must authenticate before making the call. See the section below for details.

Adding Basic Authentication

To use basic authentication, the user name and password have to be sent along with the request in the web service call. Lets see how to do it using the following code:

public void testHelloWorldSoap() throws Exception {
HelloWorld helloWorld = new HelloWorldProxy().getHelloWorld();
((org.apache.axis.client.Stub)helloWorld)._setProperty(org.apache.axis.client.Call.USERNAME_PROPERTY, "ent=,user=InstanceAdminUser");
((org.apache.axis.client.Stub)helloWorld)._setProperty(org.apache.axis.client.Call.PASSWORD_PROPERTY, "password");
String response = helloWorld.getSampleWsVal().getVal2();
String hiResponse = helloWorld.sayHi("Soap World");
}

For the HelloWorld example, create a new object for the generated HelloWorldProxy and call getHelloWorld method to get the reference to the interface, here, HelloWorld, containing the methods exposed as web service. To pass basic authentication filters, use _setProperty of org.apache.axis.client.Stub by typecasting the HelloWorld reference, as shown above. Now, call the required web services.