webservices
Create a Java web service using top down approch
In the bottom up approach, we will write the java class and generates the WSDL file and other dependent components. The same will be deployed into the web containers.
In Top down approach, Architects will write the WSDL file based on the requirements. Developer need to make the corresponding service implementation using the WSDL provided. This post will explain how to create a service using the WSDL file.
Step 1: Create a dynamic or java project as mentioned here
Here, I have created a sample web dynamic project with the name SampleWS as given below.
Step 2: generate the service using top down approach
Right click on the SamplWS project name -> New -> Other
Select the Web Service from the wizard as below and click on Finish button.
Select the Web service type as ‘Top down Java bean Web service’ and provide the WSDL url in the Service definition drop down and click on Finish button.
Sample WSDL URL is: http://localhost:8080/SampleWebService/wsdl/Calculator.wsdl
Your Web service is ready with the Java bean methods as below and the Final folder structure looks like below:
Write the business logic into the Service class as given below:
Generated class:
/**
* CalculatorSoapBindingImpl.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/package in.malliktalksjava;
public class CalculatorSoapBindingImpl implements in.malliktalksjava.Calculator{
public int addition(int var1, int var2) throws java.rmi.RemoteException {
return -3;
}public int multiplication(int var1, int var2) throws java.rmi.RemoteException {
return -3;
}public int division(int var1, int var2) throws java.rmi.RemoteException {
return -3;
}}
Implemented class:
/**
* CalculatorSoapBindingImpl.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/package in.malliktalksjava;
public class CalculatorSoapBindingImpl implements in.malliktalksjava.Calculator{
public int addition(int var1, int var2) throws java.rmi.RemoteException {
return var1+var2;
}public int multiplication(int var1, int var2) throws java.rmi.RemoteException {
return var1*var2;
}public int division(int var1, int var2) throws java.rmi.RemoteException {
return var1/var2;
}}
Deploy the application into server and use the below url as a WSDL for this to have the client.
http://localhost:8080/SampleWebService/wsdl/Calculator.wsdl
Other Useful Links:
Click here to know more about webservices
Click here to know more about RESTfull web services.
Click here for Web services Question and Answers.
Click here to know how to write web service client suing java.
Write a Client for web service
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.
What are the differences between SOAP WS and RESTful WS?
SOAP Web Services | RESTfull Web Services |
The SOAP WS supports both remote procedure call (i.e. RPC) and message oriented middle-ware (MOM) integration styles. | The Restful Web Service supports only RPC integration style. |
The SOAP WS is transport protocol neutral. Supports multiple protocols like HTTP(S), Messaging, TCP, UDP SMTP, etc. | The REST is transport protocol specific. Supports only HTTP or HTTPS protocols. |
The SOAP WS permits only XML data format. You define operations, which tunnels through the POST. The focus is on accessing the named operations and exposing the application logic as a service. | The REST permits multiple data formats like XML, JSON data, text, HTML, etc. Any browser can be used because the REST approach uses the standard GET, PUT, POST, and DELETE Web operations. The focus is on accessing the named resources and exposing the data as a service. REST has AJAX support. It can use the XMLHttpRequest object. Good for stateless CRUD (Create, Read, Update, and Delete) operations.GET – represent()POST – acceptRepresention()
PUT – storeRepresention() DELETE – removeRepresention() |
SOAP based reads cannot be cached. | REST based reads can be cached. Performs and scales better. |
SOAP WS supports both SSL security and WS-security, which adds some enterprise security features like maintaining security right up to the point where it is needed, maintaining identities through intermediaries and not just point to point SSL only, securing different parts of the message with different security algorithms, etc. | The REST supports only point-to-point SSL security. The SSL encrypts the whole message, whether all of it is sensitive or not. |
The SOAP has comprehensive support for both ACID based transaction management for short-lived transactions and compensation based transaction management for long-running transactions. It also supports two-phase commit across distributed resources. | The REST supports transactions, but it is neither ACID compliant nor can provide two phase commit across distributed transactional resources as it is limited by its HTTP protocol. |
The SOAP has success or retries logic built in and provides end-to-end reliability even through SOAP intermediaries. | REST does not have a standard messaging system, and expects clients invoking the service to deal with communication failures by retrying. |
Other Useful links:
Click here to know more about webservices
Click here to know more about RESTfull web services.
Click here for Web services Question and Answers.
Click here to know how to create a SOAP web service.
Click here to know how to write web service client suing java.