Generate Client From WSDL
In this tutorial, we will learn how to generate client side java code from WSDL file. We will use the web service created in the previous chapter using apache axis2.
Note -> Before doing this tutorial, you have to make sure the web service you created in the previous chapter is running. If you have not created the web service yet, create now using apache axis 2.
Step 1 : Locate the WSDL
Assuming web service is running on our local machine as we created in the previous chapter. If you have not created it yet, create now. Url to the wsdl file is as below. Hit the below url and confirm that the wsdl file is accessible from the browser.
http://localhost:8080/calculation-soap-web-service/services/CalculationService?wsdl
Step 2 : Create a maven project with below plugin
pom.xml
Note -> Make sure you put the correct url in <wsdlUrl> element of plugin as mentioned below
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sks</groupId>
<artifactId>CalculationServiceClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>wsimport-from-jdk</id>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<wsdlUrls>
<wsdlUrl>http://localhost:8080/calculation-soap-web-service/services/CalculationService?wsdl</wsdlUrl>
</wsdlUrls>
<keep>true</keep>
<packageName>com.sks.generated</packageName>
<sourceDestDir>src/main/java</sourceDestDir>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 3 : Please note that you need to have jdk 1.8 installed on your laptop. We will point that jdk. This example doesn’t work with the jdk 1.8 embedded inside STS/eclipse. So, it is necessary that we have a separate installation of jdk1.8
Make sure you are pointing to the jdk1.8 on your laptop. Refer the below screenshots.
Step 3 : Run As -> maven install
As the build gets successful, client classes would be generated under the package com.sks.generated as specified in pom.xml
Client classes
Step 4 : Write the main class for testing the web service
CalculationServiceTestingClient.java
Few things to understand here in the below class CalculationServiceTestingClient are :
- Create the object of the class from the generated classes that extends Service class. In this example that class is CalculationService – CalculationService service = new CalculationService();
Refer below screenshot to find your service class
2. Get the interface that exposes operations. In this case, that interface is CalculationServicePortType. CalculationServicePortType ser = service.getCalculationServiceHttpSoap11Endpoint(); Refer the below screenshot
CalculationServiceTestingClient.java
package com.sks.main;
import com.sks.generated.CalculationService;
import com.sks.generated.CalculationServicePortType;
import com.sks.generated.Operand;
public class CalculationServiceTestingClient {
public static void main(String[] args) {
CalculationService service = new CalculationService();
CalculationServicePortType ser = service.getCalculationServiceHttpSoap11Endpoint();
Operand op = new Operand();
op.setNum1(60);
op.setNum2(80);
int result = ser.add(op);
System.out.println("Result = " + result);
int multiResult = ser.multiply(op);
System.out.println("Result of multiplication = " + multiResult);
}
}
Step 5 : Run As -> Java application