REST API Interview Questions: Click Here
REST API Interview Questions: Click Here
© Copyright by Interviewbit
Contents
Conclusion
43. Conclusion
3. What is URI?
Uniform Resource Identifier is the full form of URI which is used for identifying each
resource of the REST architecture. URI is of the format:
<protocol>://<service-name>/<ResourceType>/<ResourceID>
URN: Uniform Resource Name identifies the resource by means of a name that is
both unique and persistent.
URN doesn’t always specify where to locate the resource on the internet.
They are used as templates that are used by other parsers to identify the
resource.
These follow the urn scheme and usually prefixed with urn: .
Examples include
urn:isbn:1234567890 is used for identification of book based on the
ISBN number in a library application.
urn:mpeg:mpeg7:schema:2001 is the default namespace rules for
metadata of MPEG-7 video.
Whenever a URN identifies a document, they are easily translated into a
URL by using “resolver” a er which the document can be downloaded.
URL: Uniform Resource Locator has the information regarding fetching of a
resource from its location.
Examples include:
http://abc.com/samplePage.html
ftp://sampleServer.com/sampleFile.zip
file:///home/interviewbit/sampleFile.txt
URLs start with a protocol (like p, http etc) and they have the information
of the network hostname (sampleServer.com) and the path to the
document(/samplePage.html). It can also have query parameters.
The REST architecture is designed in such a way that the client state is not
maintained on the server. This is known as statelessness. The context is provided by
the client to the server using which the server processes the client’s request. The
session on the server is identified by the session identifier sent by the client.
The POST, GET, PUT, DELETE corresponds to the create, read, update, delete
operations which are most commonly called CRUD Operations.
GET, HEAD, OPTIONS are safe and idempotent methods whereas PUT and DELETE
methods are only idempotent. POST and PATCH methods are neither safe nor
idempotent.
The technique of sending a message from the REST client to the REST server in the
form of an HTTP request and the server responding back with the response as HTTP
Response is called Messaging. The messages contained constitute the data and the
metadata about the message.
SOAP REST
SOAP is a protocol
REST is an architectural design
used to implement
pattern for developing web services
web services.
SOAP specifies
standards that are REST defines standards but they
meant to be followed need not be strictly followed.
strictly.
12. While creating URI for web services, what are the best
practices that needs to be followed?
Below is the list of best practices that need to be considered with designing URI for
web services:
While defining resources, use plural nouns. Example: To identify user resource,
use the name “users” for that resource.
While using the long name for resources, use underscore or hyphen. Avoid using
spaces between words. For example, to define authorized users resource, the
name can be “authorized_users” or “authorized-users”.
The URI is case-insensitive, but as part of best practice, it is recommended to use
lower case only.
While developing URI, the backward compatibility must be maintained once it
gets published. When the URI is updated, the older URI must be redirected to the
new one using the HTTP status code 300.
Use appropriate HTTP methods like GET, PUT, DELETE, PATCH, etc. It is not
needed or recommended to use these method names in the URI. Example: To
get user details of a particular ID, use /users/{id} instead of /getUser
Use the technique of forward slashing to indicate the hierarchy between the
resources and the collections. Example: To get the address of the user of a
particular id, we can use: /users/{id}/address
Idempotent methods ensure that the responses to a request if called once or ten
times or more than that remain the same. This is equivalent to adding any
number with 0.
REST provides idempotent methods automatically. GET, PUT, DELETE, HEAD,
OPTIONS, and TRACE are the idempotent HTTP methods. POST is not
idempotent.
POST is not idempotent because POST APIs are usually used for creating a
new resource on the server. While calling POST methods N times, there will
be N new resources. This does not result in the same outcome at a time.
Methods like GET, OPTIONS, TRACE, and HEAD are idempotent because
they do not change the state of resources on the server. They are meant for
resource retrieval whenever called. They do not result in write operations
on the server thereby making it idempotent.
PUT methods are generally used for updating the state of resources. If you
call PUT methods N times, the first request updates the resource and the
subsequent requests will be overwriting the same resource again and again
without changing anything. Hence, PUT methods are idempotent.
DELETE methods are said to be idempotent because when calling them for
N times, the first request results in successful deletion (Status Code 200),
and the next subsequent requests result in nothing - Status Code 204. The
response is different, but there is no change of resources on the server-side.
However, if you are attempting to delete the resource present, at last,
every time you hit the API, such as the request DELETE /user/last
which deletes the last user record, then calling the request N times
would delete N resources on the server. This does not make DELETE
idempotent. In such cases, as part of good practices, it is advisable to
use POST requests.
REST AJAX
REST-
AJAX - Asynchronous javascript and
Representational
XML
State Transfer
REST is an
architectural pattern
AJAX is used for dynamic updation of
for developing client-
UI without the need to reload the
server
page.
communication
systems.
Method/Verb − This part tells what methods the request operation represents.
Methods like GET, PUT, POST, DELETE, etc are some examples.
URI − This part is used for uniquely identifying the resources on the server.
HTTP Version − This part indicates what version of HTTP protocol you are using.
An example can be HTTP v1.1.
Request Header − This part has the details of the request metadata such as
client type, the content format supported, message format, cache settings, etc.
Request Body − This part represents the actual message content to be sent to
the server.
Response Status Code − This represents the server response status code for the
requested resource. Example- 400 represents a client-side error, 200 represents a
successful response.
HTTP Version − Indicates the HTTP protocol version.
Response Header − This part has the metadata of the response message. Data
can describe what is the content length, content type, response date, what is
server type, etc.
Response Body − This part contains what is the actual resource/message
returned from the server.
<protocol>://<application-name>/<type-of-resource>/<id-of-resource>
PUT POST
POST method is
PUT methods are used to request the
used to request
server to store the enclosed entity in
the server to
request. In case, the request does not
store the
exist, then new resource has to be
enclosed entity
created. If the resource exists, then the
in the request as
resource should get updated.
a new resource.
POST methods
PUT methods are idempotent. are not
idempotent.
Responses are
not cacheable
unless the
The responses are not cached here response
despite the idempotency. explicitly
specifies Cache-
Control fields in
the header.
Message transmission
Communication is slower
happens very faster than
here.
REST API
Page 25 © Copyright by Interviewbit
REST API Interview Questions
The request flow difference between the REST and Web Socket is shown below:
Payload refers to the data passes in the request body. It is not the same as the request
parameters. The payload can be sent only in POST methods as part of the request
body.
PUT yes no
POST no no
DELETE yes no
PATCH no no
JAX-RS stands for Java API for RESTful Web services. They are nothing but a set of
Java-based APIs that are provided in the Java EE which is useful in the
implementation and development of RESTful web services.
Features of JAX-RS are:
POJO-based: The APIs in the JAX-RS is based on a certain set of annotations,
classes, and interfaces that are used with POJO (Plain Old Java Object) to expose
the services as web services.
HTTP-based: The JAX-RS APIs are designed using HTTP as their base protocol.
They support the HTTP usage patterns and they provide the corresponding
mapping between the HTTP actions and the API classes.
Format Independent: They can be used to work with a wide range of data types
that are supported by the HTTP body content.
Container Independent: The APIs can be deployed in the Java EE container or a
servlet container such as Tomcat or they can also be plugged into JAX-WS (Java
API for XML-based web services) providers.
import javax.ws.rs.Path;
/**
* InterviewBitService is a root resource class that is exposed at 'resource_service' pa
*/
@Path('resource_service')
public class InterviewBitService {
// Defined methods
}
They are the runtime annotations in the JAX-RS library that are applied to Java
methods. They correspond to the HTTP request methods that the clients want to
make. They are @GET, @POST, @PUT, @DELETE, @HEAD.
Usage Example:
import javax.ws.rs.Path;
/**
* InterviewBitService is a root resource class that is exposed at 'resource_service' pa
*/
@Path('resource_service')
public class InterviewBitService {
@GET
public String getRESTQuestions() {
// some operations
}
}
Let us understand this with the help of a random example. We know that the
Future interface from the java.util.concurrent has the below functions
available:
package java.util.concurrent;
public interface Future<V> {
// informs the executor to stop the thread execution
boolean cancel(boolean mayInterruptIfRunning);
Let us consider we have this function below which is used for processing 2 Ids
parallelly.
In the above example, we see that there are 2 separate requests getting executed
parallelly. For the first future object, we await the javax.ws.rs.core.Response
indefinitely using the get() method until we get the response. For the second future
object, we wait for the response only for 2 seconds and if we do not get within 2
seconds, then the get() method throws TimeoutException. We can also use the
isDone() method or isCancelled() method to find out whether the executors have
completed or cancelled.
36. List the key annotations that are present in the JAX-RS API?
@Path - This specifies the relative URI path to the REST resource.
@GET - This is a request method designator which is corresponding to the HTTP
GET requests. They process GET requests.
@POST - This is a request method designator which is corresponding to the
HTTP POST requests. They process POST requests.
@PUT - This is a request method designator which is corresponding to the HTTP
PUT requests. They process PUT requests.
@DELETE - This is a request method designator which is corresponding to the
HTTP DELETE requests. They process DELETE requests.
@HEAD - This is a request method designator which is corresponding to the
HTTP HEAD requests. They process HEAD requests.
@PathParam - This is the URI path parameter that helps developers to extract
the parameters from the URI and use them in the resource class/methods.
@QueryParam - This is the URI query parameter that helps developers extract
the query parameters from the URI and use them in the resource class/methods.
@Produces - This specifies what MIME media types of the resource
representations are produced and sent to the client as a response.
@Consumes - This specifies which MIME media types of the resource
representations are accepted or consumed by the server from the client.
@Controller @RestController
It is used in case of
It is mostly used in Spring MVC
RESTful web service that
service where model data needs
returns object values
to rendered using view.
bound to response body.
@RestController
@Controller provides control annotation has no such
and flexibility over how the flexibility and writes all
response needs to be sent. the results to the
response body.
Conclusion
43. Conclusion
We have seen what are the most commonly asked questions on RESTful web services
during an interview. REST APIs have become a very important tool in the so ware
industry. Developing RESTful web services that are scalable and easily maintainable is
considered an art. As the industry trends increase, the REST architecture would
become more concrete and the demand for developers who know the development
of RESTful web services would increase steadily.
References:
To learn more about REST, you can refer to the below 2 links:
https://restcookbook.com/
https://www.restapitutorial.com/
Css Interview Questions Laravel Interview Questions Asp Net Interview Questions