Showing posts with label SOAP. Show all posts
Showing posts with label SOAP. Show all posts

Simple Web Service using SOAP


In one of our earlier tutorial we have seen Web Services in Java and we have discussed about SOAP Web Service and RESTful Web Services along with their differences. As further we will see a simple example for how to create Web Service server and client using SOAP. For this we will use eclipse J2EE IDE and Apache Tomcat as web server.

First we will create simple Server which will take 2 numbers as input and returns the addition of the program. Here by using Eclipse its very simple to create a service and to deploy and publish a service. Everything we can do it just by clicks rather than doing it lot manually. And also using IDE's will make life more easy than manually.

Server Program:

STEP - 1 : Create a Dynamic Web Project (File -> New -> Dynamic Web Project). Create your project by setting your Target run-time server to installed Apache Tomcat web server. In demo I have used Apache Tomcat 7.0 and you can decide your version according to your need. 

Simple Web Service using SOAP


STEP - 2 : Create a new class file under Java Resource by giving package and class name. Then implement the server side method to publish.  

Simple Web Service using SOAP


STEP - 3 : After finishing the server side method implementation, select New -> Others and type "web" in Wizards, where you can see Web Service under Web Services list. 

Simple Web Service using SOAP


STEP - 4 : Click next and select the class which you implemented the method to publish under Service implementation drop down box. As already we are ready with Develop service, so we have to
Assemble service
Deploy service
Install service
Start service 
Test service

to make Web Service server ready with publishing the service which we have implemented. So that just passing the WSDL (Web Services Description Language) to client they can understand the server implementation and can consume our service easily. Basically its XML which used to describe the functionality offered or published by a web service. For more details on WSDL you can refer to WSDL wiki.

Simple Web Service using SOAP


STEP - 5 : Our Web Service server created successfully and we are ready to test our service. In next screen you can see SOAP Binding and service created with WSDL. You can pass input parameter and can test the service as below 

Simple Web Service using SOAP


And also we can see SOAP request and response by switching from form to source in result tab as below. 

Simple Web Service using SOAP


STEP - 6 : Next you can check your WSDL file have created correctly or not in your web browser by passing. http://localhost:8080/MyServer/services/MyClass?wsdl

Simple Web Service using SOAP



We have done with server implementation by creating the service and publishing with the help of Eclipse of IDE. Next important that we are going to localhost as our web server so we should not stop the server which running. In real-time we will deploy our service in some web servers and we will access our service through internet. Where as for our demo we are localhost for both server and client. 

Next go-head and create the client and consume the service which we have published above. For that we need to create the client application given below. 


Client Program:

STEP - 7 :  Create a Dynamic Web Project (File -> New -> Dynamic Web Project).

Simple Web Service using SOAP


STEP - 8 : Next we need to create stub from WSDL file. For this server where we have published need to be running and as discussed above we haven't stopped the server which we have started while creating our server. 
For creating Web Service Client we need to select New -> Other and type "web" in the Wizards as given below and select the Web Service Client under Web Services tab.

Simple Web Service using SOAP


STEP - 9 : Click Next and pass the WSDL url in Service definition text box and set as Test client and click Finish which will read the WSDL file and generate the java stub code. 

Simple Web Service using SOAP


Once you finished we can see the Java stub created along with Service and Proxy classes. Next we can see the list of method(s) published by server. By selecting the method we can test directly from eclipse by giving inputs as below image.

Simple Web Service using SOAP


STEP - 10 : Next we need to write a client program to invoke the remote method using SOAP Web Services. Create new class called MyClient as below image.

Simple Web Service using SOAP


STEP - 11 : Just implement the client by using endpoint URL and service instance and test your Web Service using SOAP.

Simple Web Service using SOAP







Web Services in Java


In this tutorial we will see about Web Services in Java.


What are Web Services?


Web services are client and server applications that communicate over the World Wide Web's (WWW) HyperText Transfer Protocol (HTTP). As described by the World Wide Web Consortium (W3C), web services provide a standard means of interoperating between software applications running on a variety of platforms and frameworks. Web services are characterized by their great interoperability and extensibility, as well as their machine-processable descriptions, thanks to the use of XML. Web services can be combined in a loosely coupled way to achieve complex operations. Programs providing simple services can interact with each other to deliver sophisticated added-value services. By Oracle Docs.


Before Web Services?

Before Web Services introduced we were using RMI (Remote Method Invocation), CORBA etc., to communicate third part services and methods. With the release of JDK version 1.1, Java has its own, built-in native ORB, called RMI (Remote Method Invocation). Where CORBA is an integration technology, not a programming technology which used in Java to communicate between other languages like C, C++, Ada, COBOL etc., Instead of digging to RMI or other client/ server we will focus only on Web Services in this tutorial. 


Types of Web Services?

There are 2 tyes of Web Services,
1. SOAP based (Simple Object Access Protocol) Web Services 
2. RESTful Web Services 


SOAP Web Services (JAX-WS)

SOAP web services use XML messages that follow the Simple Object Access Protocol (SOAP) standard, an XML language defining a message architecture and message formats. Such systems often contain a machine-readable description of the operations offered by the service, written in the Web Services Description Language (WSDL), an XML language for defining interfaces syntactically. Basically Client and Server communication will be in XML standard formats which gives flexibility to use any platform (Java, .NET, etc.,) and to use any OS (Windows, Unix, etc.,). Generally Server can be in any language and client can be any language where communication will be in common XML formats and standards.


RESTful Web Services (JAX-RS)


JAX-RS provides the functionality for Representational State Transfer (RESTful) web services in Java. REST is well suited for basic, ad hoc integration scenarios. RESTful web services, often better integrated with HTTP than SOAP-based services are, do not require XML messages or WSDL service–API definitions as like SOAP. By Oracle Docs.


Difference between SOAP and RESTful

  • SOAP is a Object oriented and REST is a resource oriented.
  • In place of security SOAP supports SSL and WS-Security and REST supports only SSL.
  • As performance wise SOAP will be better than REST
  • SOAP doesn't support caching and REST supports.
  • SOAP is heavy in message size with WS specific markup. where as REST is lightweight and no extra XML markups added. 
  • SOAP supports text and binary encoding and REST supports only text encoding.
  • SOAP uses WSDL to describe the services which we have exposed and in REST there is no formal definition.
  • Complexity of SOAP uses frameworks to develop and on other side REST is very simple in development without any frameworks. 
  • Protocols supported in SOAP are HTTP, SMTP, JMS and REST supports only HTTP.


According to project needs development team need to decide whether they need to go with SOAP or REST.