Restful web services interview questions
In this tutorial we are going to have quick look on all the important Restful web services interview questions.
REST is an architectural style which was brought in by Roy Fielding in 2000 in his doctoral thesis.
In the REST architecture style, clients and servers exchange representations of resources by using a standardized interface and protocol.REST isn’t protocol specific, but when people talk about REST they usually mean REST over HTTP.
In the web services terms, REpresentational State Transfer (REST) is a stateless client-server architecture in which the web services are viewed as resources and can be identified by their URIs.
Web services client uses that URI to access the resource.
It consists of two components REST server which provides access to the resources and a REST client which accesses and modify the REST resources.
Some important features of Restful web services are:
- Resource identification through URI:Resources are identified by their URIs (typically links on internet). So, a client can directly access a RESTful Web Services using the URIs of the resources (same as you put a website address in the browser’s address bar and get some representation as response).
- Uniform interface: Resources are manipulated using a fixed set of four create, read, update,delete operations:PUT, GET, POST, and DELETE.
- Client-Server: A clear separation concerns is the reason behind this constraint. Separating concerns between the Client and Server helps improve portability in the Client and Scalability of the server components.
- Stateless: each request from client to server must contain all the information necessary to understand the request, and cannot take advantage of any stored context on the server.
Cache: to improve network efficiency responses must be capable of being labeled as cacheable or non-cacheable.
- Named resources – the system is comprised of resources which are named using a URL. Interconnected resource representations – the representations of the resources are interconnected using URLs, thereby enabling a client to progress from one state to another.
- Layered components – intermediaries, such as proxy servers, cache servers, gateways, etc, can be inserted between clients and resources to support performance, security, etc.
- Self-descriptive messages: Resources are decoupled from their representation so that their content can be accessed in a variety of formats, such as HTML, XML, plain text, PDF, JPEG, JSON, and others.
RESTful web services use HTTP protocol methods for the operations they perform. Some important Methods are:
- GET : It defines a reading access of the resource without side-effects.This operation is idempotent i.e.they can be applied multiple times without changing the result
- PUT : It is generally used for updating resource. It must also be idempotent.
- DELETE : It removes the resources. The operations are idempotent i.e. they can get repeated without leading to different results.
- POST : It is used for creating a new resource. It is not idempotent.
Idempotent means result of multiple successful request will not change state of resource after initial application.
For example : Delete is idempotent method because when you first time use delete, it will delete the resource (initial application) but after that, all other request will have no result because resource is already deleted. Get, put and delete are HTTP Idempotent methods.
- POST :It is used for creating a new resource. It is not idempotent.
- PUT : It is generally used for updating resource. It is idempotent. Idempotent means result of multiple successful request will not change state of resource after initial application
What happens if resources are shared by multiple clients? Do you need to make it thread safe explicitly?
New resource instance is created for each request, so you don’t need to implement thread safety or synchronization aid. It is by default thread safe.
Java API for RESTful Web Services (JAX-RS), is a set if APIs to developer REST service. JAX-RS is part of the Java EE6, and make developers to develop REST web application easily.
There are multiple Rest framework that can be used to create Restful web services such as
Spring Rest webservices
Some of important annotations which are used for creating web services are:
- @Path : This is used to set path for URI at class level or method level
- @GET,@POST,@PUT,@DELETE : There are annotations corresponds to HTTP methods
- @Produces(MediaType.TEXT_XML [, more-types ]): @Produces defines which MIME type is delivered by a method
- @PathParam: Used to inject values from the URL into a method parameter.
- @Consumes(MediaType.TEXT_XML) : @Cosumes defines which MIME type will be consumed by the method.
No, Get should be used only for resource retrieval and not for resource creation.
You require a restful client to test restful web services. You can use:
- Postman for chrome browser
- poster for firefox