Below steps explains how to write a web service client in java using STS IDE.
Step 1: Create a Java project using the steps mentioned here.
Step 2: Generate the stubs for the Java web service using below steps
Mouse Right click on Client project and select New -> Other
Select the Web service client from the wizard
Provide the service WSDL url in the Service Definition text box and click on finish button.
Web service client stubs will be generated into the package and final folder structure looks below.
Write the Client class using the stubs and test the client project.
Use the below sample code to write the client:
package in.malliktalksjava.client;
import java.rmi.RemoteException;
import in.malliktalksjava.Calculator;
import in.malliktalksjava.CalculatorServiceLocator;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;/**
* @author Javatutorials
* @since version 1.0
*
*/
public class SampleWSClient {/**
* @param args
*/
public static void main(String[] args) {SampleWSClient sc = new SampleWSClient();
sc.callCalculatorWebservice();
}/**
* used to call web service
*/
public void callCalculatorWebservice(){String wsdl = “http://localhost:8080/SampleWebService/wsdl/Calculator.wsdl”;
QName queue = new QName(“http://malliktalksjava.in”, “CalculatorService”);try {
//create the servicelocator object
CalculatorServiceLocator calServiceLoc = new CalculatorServiceLocator(wsdl, queue);
//create the service object
Calculator calculator = calServiceLoc.getCalculator();
//call the service methods
System.out.println(“addition result : “+calculator.addition(10, 11));
System.out.println(“division result : “+calculator.division(10, 5));
System.out.println(“multiplication result : “+calculator.multiplication(10, 10));
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
With this your client application is ready to use.
Other Useful links:
Click here to know how to create the web service project.
Click here to know the difference between SOAP and RESTfull web services.
Filed under: Web Services Tagged: client stubs, Generating webservice client stubs, how to generated web service client stubs in STS, Java, java project, Java Web services, javatutorials, Service-oriented architecture, SOAP, software, Web service, web service client, Web Service Description Language, webservices, writing webservice client using STS, WSDL
