REST and SOAP Web Services
What is a Web Service
Web service is a collection of open protocols and standards used for exchanging data between applications or systems. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computer. This interoperability (e.g., between Java and Python, or Windows and Linux applications) is due to the use of open standards.
REST
·
While REST stands
for Representational State Transfer, which is an architectural style for
networked hypermedia applications, it is primarily used to build Web services that
are lightweight, maintainable, and scalable. A service based
on RESTis
called a RESTful
service.
·
Many developers found SOAP cumbersome and hard
to use. For example, working with SOAP in JavaScript means writing a ton of
code to perform extremely simple tasks because you must create the required XML
structure absolutely every time.
·
Web services based on REST Architecture are known
as RESTful Web Services. These web services use HTTP methods to implement the
concept of REST architecture. A RESTful web service usually defines a URI
(Uniform Resource Identifier), which is a service that provides resource
representation such as JSON and a set of HTTP Methods.
HTTP Methods
The following HTTP methods are most commonly
used in a REST based architecture.
·
GET − Provides a read only access to a
resource.
·
PUT − Used to create a new resource.
·
DELETE − Used to remove a resource.
·
POST − Used to update an existing resource or
create a new resource.
·
OPTIONS − Used to get the supported operations on
a resource.
Code(Java) Ex :
package com.tutorialspoint;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
public class UserService {
UserDao userDao = new UserDao();
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers(){
return userDao.getAllUsers();
}
}
Resource URI Ex :
·
To insert (create) a new student in the system, we might use:
POST http://www.sliit.com/students
POST http://www.sliit.com/students
·
To read a student with Student ID# IT15142610:
GET http://www.sliit.com/students/ IT15142610 The same URI would be used for PUT and DELETE, to update and delete, respectively.
GET http://www.sliit.com/students/ IT15142610 The same URI would be used for PUT and DELETE, to update and delete, respectively.
·
Here are proposed URIs for subjects:
POST http://www.sliit.com/subjects for creating a new subject.
POST http://www.sliit.com/subjects for creating a new subject.
·
GET|PUT|DELETE http://www.sliit.com/subjects/66432
for reading, updating, deleting subject 66432, respectively.
for reading, updating, deleting subject 66432, respectively.
SOAP
·
(Simple
Object Access Protocol) is a messaging protocol that allows programs that
run on disparate operating systems (such as Windows and Linux) to communicate
using Hypertext Transfer Protocol (HTTP) and its Extensible Markup Language
(XML).
·
Microsoft originally developed SOAP to take the
place of older technologies that don’t work well on the Internet such as the
Distributed Component Object Model (DCOM) and Common Object Request Broker
Architecture (CORBA). These technologies fail because they rely on binary
messaging; the XML messaging that SOAP employs works better over the Internet.
·
The XML used to make requests and receive
responses in SOAP can become extremely complex. In some programming languages,
you need to build those requests manually, which becomes problematic because
SOAP is intolerant of errors. However, other languages can use shortcuts that
SOAP provides; that can help you reduce the effort required to create the
request and to parse the response. In fact, when working with .NET languages,
you never even see the XML.
A SOAP message is an ordinary XML document containing the
following elements −
·
Envelope − Defines the start and the end of the message. It is a mandatory
element.
·
Header − Contains any optional attributes of the message used in processing
the message, either at an intermediary point or at the ultimate end-point. It
is an optional element.
·
Body − Contains the XML data comprising the message being sent. It is a
mandatory element.
·
Fault − An optional Fault element that provides information about errors
that occur while processing the message.
The following block depicts the general
structure of a SOAP message −
<?xml
version="1.0"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope"
SOAP-ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<SOAP-ENV:Header>
...
...
</SOAP-ENV:Header>
<SOAP-ENV:Body>
...
...
<SOAP-ENV:Fault>
...
...
</SOAP-ENV:Fault>
...
</SOAP-ENV:Body>
</SOAP_ENV:Envelope>
Ex
:
Here is the SOAP request −
POST /Quotation HTTP/1.0
Host: www.xyz.org
Content-Type: text/xml; charset=utf-8
Content-Length: nnn
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope" SOAP-ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding" >
<SOAP-ENV:Body xmlns:m="http://www.xyz.org/quotations" >
<m:GetQuotation>
<m:QuotationsName>MiscroSoft</m:QuotationsName>
</m:GetQuotation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
A corresponding SOAP response looks like −
HTTP/1.0 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: nnn
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope" SOAP-ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding" >
<SOAP-ENV:Body xmlns:m="http://www.xyz.org/quotation" >
<m:GetQuotationResponse>
<m:Quotation>Here is the quotation</m:Quotation>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


Comments
Post a Comment