Rest Interview Questions and Answers

1) What are webservices?

A 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.

2) What is REST?

REST stands for <strong>Representational State Transfer.</strong> It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.<br/> It is an architecture style for designing networked applications.It is a lightweight alternative to Web Services and RPC. REST design operations are self-contained, and each request carries with it (transfers) all the information (state) that the server needs in order to complete it.

3) What are the HTTP methods used in REST for CRUD operation?

Following well known HTTP methods are commonly used in REST based architecture:<br/> <strong>GET </strong>: Provides a read only access to a resource.<br/> <strong>PUT </strong>: Used to create a new resource.<br/> <strong>DELETE </strong>: Ued to remove a resource.<br/> <strong>POST </strong>: Used to update a existing resource or create a new resource.<br/> <strong>OPTIONS </strong>: Used to get the supported operations on a resource.

4) What are the features of REST as a LightWeight Web Services?

Much like Web Services, a REST service is:<br/> Platform-independent (you don't care if the server is Unix, the client is a Mac, or anything else),<br/> Language-independent (C# can talk to Java, etc.),<br/> Standards-based (runs on top of HTTP), and<br/> Can easily be used in the presence of firewalls.<br/> Like Web Services, REST offers no built-in security features, encryption, session management, QoS guarantees, etc. But also as with Web Services, these can be added by building on top of HTTP:

5) What is a Resource in REST?

REST architecture treats every content as a resource. These resources can be text files, html pages, images, videos or dynamic business data. REST Server simply provides access to resources and REST client accesses and modifies the resources. Here each resource is identified by URIs/ global IDs.

6) Which type of Webservices methods are to be idempotent?

PUT and DELETE operations are idempotent.

7) How REST is simpler than SOAP. Give an example.

Using Web Services and SOAP, the request would look something like this:<br/> &lt;?xml version="1.0"?&gt;<br/> &lt;soap:Envelope<br/> xmlns:soap=""<br/> soap:encodingStyle=""><br/> &lt;soap:body pb=""&gt;<br/> &lt;pb:GetUserDetails&gt;<br/> &lt;pb:UserID&gt;12345&lt;/pb:UserID&gt;<br/> &lt;/pb:GetUserDetails&gt;<br/> &lt;/soap:Body&gt;<br/> &lt;/soap:Envelope&gt;<br/> (The details are not important; this is just an example.) The entire shebang now has to be sent (using an HTTP POST request) to the server. The result is probably an XML file, but it will be embedded, as the "payload", inside a SOAP response envelope.<br/> And with REST? The query will probably look like this:<br/> <strong></strong><br/> Note that this isn't the request body -- it's just a URL. This URL is sent to the server using a simpler GET request, and the HTTP reply is the raw result data -- not embedded inside anything, just the data you need in a way you can directly use.

8) What are the different responses of REST?

REST is not limited to any particular type of respnses. unlike SOAP services, REST is not bound to XML in any way. Possible formats include CSV (comma-separated values) and JSON (JavaScript Object Notation).

9) What are the different status codes used in REST?

1. 1XX - information.<br/> 2. 2XX - Success<br/> 3. 3XX - Redirection<br/> 4.4XX - Client Error<br/> 5. 5XX - Server Error

10) What is the use of Accept and Content-Type Headers in HTTP Request?

These are important headers in Restful web services. Accept headers tells web service what kind of response client is accepting, so if a web service is capable of sending response in XML and JSON format and client sends Accept header as “application/xml” then XML response will be sent. For Accept header “application/json”, server will send the JSON response.<br/> Content-Type header is used to tell server what is the format of data being sent in the request. If Content-Type header is “application/xml” then server will try to parse it as XML data. This header is useful in HTTP Post and Put requests.

11) Compare between REST and AJAX.

AJAX is a popular web development technique that makes web pages interactive using JavaScript.<br/> In AJAX, requests are sent to the server using XMLHttpRequest objects. The response is used by the JavaScript code to dynamically change the current page.

12) What difference between Jersey vs jax-rsin context to REST?

<strong>1. </strong>JAX-RS is an specification (just a definition) and Jersey is a JAX-RS implementation. For example: JAX-RS gives you a set of interfaces (standard Java interfaces) which are implemented by Jersey. So that set of interfaces (or classes) are needed as a dependency to be implemented.<br/> <strong>2. </strong>In many ways, AJAX applications follow the REST design principles. Each XMLHttpRequest can be viewed as a REST service request, sent using GET. And the response is often in JSON, a popular response format for REST

13) What are the different REST design guidlines?

<strong>1.</strong> Do not use "physical" URLs.<br/> <strong>2.</strong> Queries should not return an overload of data. If needed, provide a paging mechanism. For example, a "product list" GET request should return the first n products (e.g., the first 10), with next/prev links.<br/> <strong>3.</strong> Even though the REST response can be anything, make sure it's well documented, and do not change the output format lightly (since it will break existing clients).<br/> <strong>4.</strong> Rather than letting clients construct URLs for additional actions, include the actual URLs with REST responses.

14) Difference between WSDL and WADL?

<strong>WSDL</strong> is the Web Services Description Language. It is commonly used to spell out in detail the services offered by a SOAP server. While WSDL is flexible in service binding options (for example, services can be offered via SMTP mail servers), it did not originally support HTTP operations other than GET and POST. Since REST services often use other HTTP verbs, such as PUT and DELETE, WSDL was a poor choice for documenting REST services.<br/> <strong>WADL</strong> is the Web Application Description Language. WADL is championed by Sun Microsystems. Like the rest of REST, WADL is lightweight, easier to understand and easier to write than WSDL. In some respects, it is not as flexible as WSDL (no binding to SMTP servers), but it is sufficient for any REST service and much less verbose.

15) What are the differences between no-cache, and no-store directives used along with Cache-Control header in REST?

<strong>no-cache</strong> can be set in response in order to inform client/browser that this response should not be used for caching content and any of the cache data should not be sent to server without revalidation from server.<br/> <strong>no-store</strong> is to inform client/browser as not to store any data in response in local hard disk of the machine that is used for sending the request.<br/> In case of no-cache, one can use data with revalidation, but in no-store that is no ways any data can be retrieved locally from the hard disk and data won't be available when machine if restarted.

16) what is HATEOAS in REST?

<strong>HATEOAS</strong> stand for Hypermedia As The Engine Of Application State. It provides links to resources so that client does not have to manually bookmark the links. Below is an example.<br/> {<br/> "id":1,<br/> "message":"Hello World",<br/> "author":"Dhiraj",<br/> "href":"/messages/1"<br/> }

17) What are the different approaches to implement security in REST?

There are two basic approaches here. <br/>The first is, use <strong>HTTP AUTH.</strong> The (human) user will be prompted for the credential just once (or they will be loaded from file, depending on the system at hand). The client software will compute the Base64 encoding of the credentials and will include them in each future HTTP request to the server (using the "Authorization" HTTP header).<br/> The second alternative is to create a dedicated login service, that accepts credentials and returns a <strong>token</strong>. This token should then be included, as a URL argument, to each following request (e.g., by addding "&authtoken=XYZ" to the URL). Of course, the API (including the argument's name) should be clearly defined.

18) Difference between SOAP and REST?

<strong>1.</strong> SOAP (Simple Object Access Protocol) is a standard communication protocol on top of transport protocols such as HTTP, SMTP, Messaging, TCP, UDP, etc. whereas REST is an architectural style by which data can be transmitted over transport protocol such as HTTP(S).<br/> <strong>2.</strong>SOAP only permits XML data formats whereas REST permits many different data formats like XML, JSON data, text, HTML, atom, RSS, etc. JSON is less verbose than XML and is a better fit for data and parses much faster.<br/> <strong>3. </strong>SOAP Supports both SSL security and WS-security, which adds some enterprise security features. Supports identity through intermediaries, not just point to point SSL whereas REST supports only point-to-point SSL security.

19) How would you decide what style of Web Service to use? SOAP WS or REST?

In general, a REST based Web service is preferred due to its simplicity, performance, scalability, and support for multiple data formats. SOAP is favored where service requires comprehensive support for security and transactional reliability.

20) What tools do you use to test your Web Services?

SoapUI tool for SOAP WS & RESTFul web service testing and on the browser the Firefox “poster” plugin or Google Chrome “Postman” extension for RESTFul services or Advanced Rest Client provided by Google.

21) What is Jersey?

JAX-RS is a JSR Specification defining a Java API for RESTful Web Services. <strong>Jersey</strong> is a specific implementation of JAX-RS.

22) Write a sample GET request using Jersey.

@GET<br/> @Path("/{empId}")<br/> Employee getEmployee(<strong>@PathParam("empId") String empId</strong>, <strong>@QueryParam("empName") String empName</strong>);

Is this page helpful to you? Please give us your feedback below. We would love to hear your thoughts on these articles, it will help us improve further our learning process.