Javatpoint Logo
Javatpoint Logo

Types of Web Services

There are two types of web services:

  • RESTful Web Servies
  • SOAP Web Services

RESTful Web Services

REST stands for REpresentational State Transfer. It is developed by Roy Thomas Fielding who also developed HTTP. The main goal of RESTful web services is to make web services more effective. RESTful web services try to define services using the different concepts that are already present in HTTP. REST is an architectural approach, not a protocol.

It does not define the standard message exchange format. We can build REST services with both XML and JSON. JSON is more popular format with REST. The key abstraction is a resource in REST. A resource can be anything. It can be accessed through a Uniform Resource Identifier (URI). For example:

The resource has representations like XML, HTML, and JSON. The current state is captured by representational resource. When we request a resource, we provide the representation of the resource. The important methods of HTTP are:

  • GET: It reads a resource.
  • PUT: It updates an existing resource.
  • POST: It creates a new resource.
  • DELETE: It deletes the resource.

For example, if we want to perform the following actions in the social media application, we get the corresponding results.

POST /users: It creates a user.

GET /users/{id}: It retrieve the detail of one user.

GET /users: It retrieve the detail of all users.

DELETE /users: It delete all users.

DELETE /users/{id}: It delete a user.

GET /users/{id}/posts/post_id: It retrieve the detail of a specific post.

POST / users/{id}/ posts: It creates a post for a user.

GET /users/{id}/post: Retrieve all posts for a user

HTTP also defines the following standard status code:

  • 404: RESOURCE NOT FOUND
  • 200: SUCCESS
  • 201: CREATED
  • 401: UNAUTHORIZED
  • 500: SERVER ERROR

RESTful Service Constraints

  • There must be a service producer and service consumer.
  • The service is stateless.
  • The service result must be cacheable.
  • The interface is uniform and exposing resources.
  • The service should assume a layered architecture.

Advantages of RESTful web services

  • RESTful web services are platform-independent.
  • It can be written in any programming language and can be executed on any platform.
  • It provides different data format like JSON, text, HTML, and XML.
  • It is fast in comparison to SOAP because there is no strict specification like SOAP.
  • These are reusable.
  • These are language neutral.

SOAP Web Services

REST defines an architectural approach whereas SOAP poses a restriction on the format of the XML. XML transfer data between the service provider and service consumer. Remember that SOAP and REST are not comparable.

SOAP: SOAP acronym for Simple Object Access Protocol. It defines the standard XML format. It also defines the way of building web services. We use Web Service Definition Language (WSDL) to define the format of request XML and the response XML.

For example, we have requested to access the Todo application from the Facebook application. The Facebook application sends an XML request to the Todo application. Todo application processes the request and generates the XML response and sends back to the Facebook application.

Types of Web Services

If we are using SOAP web services, we have to use the structure of SOAP.

Types of Web Services

In the above figure, the SOAP-Envelope contains a SOAP-Header and SOAP-Body. It contains meta-information needed to identify the request, for example, authentication, authorization, signature, etc. SOAP-Header is optional. The SOAP-Body contains the real XML content of request or response. In case of an error, the response server responds back with SOAP-Fault.

Let's understand the SOAP XML request and response structure.

XML Request

XML Response

Points to remember

  • SOAP defines the format of request and response.
  • SOAP does not pose any restriction on transport. We can either use HTTP or MQ for communication.
  • In SOAP, service definition typically done using Web Service Definition Language (WSDL). WSDL defines Endpoint, All Operations, Request Structure, and Response Structure.

The Endpoint is the connection point where HTML or ASP pages are exposed. It provides the information needed to address the Web Service endpoint. The operations are the services that are allowed to access. Request structure defines the structure of the request, and the response structure defines the structure of the response.






Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA