REST Service overview

What is Rest?

REST stands for Representational State Transfer

REST is an architectural style based on HTTP protocol.

Remember REST is not a protocol rather it’s an architectural style based on web standards and protocol.

In Rest, each and everything is a resource.

Each resource will have a representation, Representation can be any MIME type (pdf,json,xml etc)

What is Rest Service?

Every system will have resources and Rest services provide a way to access these resources.

Rest services provides a way for the clients to access these resources through the channel called API.

Each and every Rest service will be exposed to the clients through URL and HTTP method.

Client makes a request to the service to communicate with the resources.

Communication could be to get the resources in some representation or modify the existing resource or delete the existing resource or add the new resource etc.

Appropriate HTTP methods will be used while communicating with the service.

Rest Service Overview

Client makes a request to the REST service using URL and HTTP method and it will send the content through request body or Payload if required.

Appropriate Rest service will be invoked.

Service will process the request and send the response back to client with status code and response body.

Status code will be used by client to determine whether the response is successful or not.

Client will also use response body to consume the resource and proceed further.

Let’s represent Employee record as a resource in JSON and XML representation

Employee Resource with JSON representation.

1
2
3
4
5
{
    "ID": "1",
    "Name": "Daniel",
    "Email": "Daniel@gmail.com"
}
{
    "ID": "1",
    "Name": "Daniel",
    "Email": "Daniel@gmail.com"
}

Employee Resource with XML representation

1
2
3
4
5
<Employee>
<ID>1</ID>
<Name>Daniel</Name>
<Email>Daniel@gmail.com</Email>
</Employee>
<Employee>
<ID>1</ID>
<Name>Daniel</Name>
<Email>Daniel@gmail.com</Email>
</Employee>


Advantages of Rest service

Rest services are platform independent

Rest service supports different data formats such as XML,JSON,PDF,HTML etc.

Rest is fast compared to SAOP as it does not follow strict SOAP specification and it’s cacheable.

Rest implementation is easy compared to SOAP as it is very similar to Spring MVC controllers

Disadvantages of Rest service

REST supports only HTTP protocol whereas SOAP supports HTTP,SMTP,FTP etc.

Rest services are less secure compared to SOAP as SOAP uses WS-Security standards to encrypt the SOAP messages and REST uses HTTPS to protect its payload.

Rest is stateless as it uses HTTP and hence service will have to get information about client each time it makes a request.

When to use Rest services?

We can prefer REST service in the following scenarios

Check whether our requirement is to provide business logic or to return any resource/data.
If our service needs to return just a data then prefer Rest.

Check if our requirement needs a stateless conversation between client and server, if so prefer REST.

If client expect the response in different formats like JSON,XML,PDF etc then prefer REST.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on