REST Web Service in Java Interview Questions

Question 1:

What is differences between RESTful web services and SOAP web services ?

SOAP stands for Simple Object Access Protocol. REST stands for REpresentational State Transfer.
SOAP is a XML-based messaging protocol. REST is not a protocol but an architectural style.
SOAP has specifications for stateful implementation as well. REST follows stateless model.
SOAP uses interfaces and named operations to expose business logic. REST uses URI methods like GET, POST to expose resources.
SOAP permits only XML data format. REST is not bounded to XML so it can use Plain text, HTML, XML, JSON.
SOAP defines its own security mechanism like JAAS. REST uses security mechanism of underlying protocol.
Usage: Banking Application where security is concern. Usage:  Mobile Application, Android application as it is light weighted as compared to SOAP.
Question 2:

What is REST and RESTful web services?

REST was introduced as an architectural style for building large-scale distributed system. By "architectural style" we mean - a set of design rules that identify the kinds of components and connectors that may be used to compose a system or subsystem.

REST is the stateless client-server architecture in which resources are exposed as web service and can be identified by their URL's/URI's. By using set of remote methods client can use the resources and in return web service returns representational state of the resource.

RESTful is typically used to refer to web services implementing such an architecture.

Question 3:

What is a web service?

In this rapid development era, Organizations don't prefer to build everything of their own. They prefer to use the components which can process their data and return the processed data that can be used further.

Organizations started analysing and finding such business requirements and functionalities. Due to this now multiple organisations started creating service that are specific to any particular business model in the market and can process the input provided. 

For example Google Map, Amazon Web Service 

Web Service allows different applications from different sources to communicate with each other. Web Services are not tied to any Operating System or Programming Language. For example  Web Service built in Java can be consumed by Application built on .Net.

Web service can also be used internally in an organization network as a way to allow different application to interact with each other.

Question 4:

Can I use GET request instead of PUT to create resources?

Though we can use get method to create resource, it is bad practice to do as it will surprise the consumer to use get method to create resource.

Question 5:

What is diference between PUT and POST request?

The HTTP methods, POST and PUT serve a different purpose.

PUT puts a file or resource at a specific URI. If there's already a file or resource at that URI, PUT replaces that file or resource. If there is no file or resource there, PUT creates one.

For example: - If you know the where the product resides at i.e , you can use PUT method to update the resource.

In another hand POST method is used to add the new product if we don't know the actual resource location and we let the server decide where it should store it.

Hence PUT is idempotent, while POST is not.

Question 6:

What are the HTTP methods used in Rest Services?

Various HTTP methods used in Rest services are: 







Question 7:

What happens if Restful resources are accessed by multiple clients? Do you need to make it thread-safe?

For every incoming request, a new resource instance is created hence there is no need to make restful resource thread-safe or make it synchronized. Due to this multiple clients can access this resource concurrently.

Question 8:

How can you apply security to RESTful services?

There are two basic approaches here. The first is, use HTTP AUTH. The user will be prompted for the credential just once. 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).

The second alternative is to create a dedicated login service, that accepts credentials and returns a token. This token should then be included, as a URL argument, to each following request (e.g. by addding "&authenticationtoken=ABC" to the URL). The main advantage of the this approach is that tokens can be created with an expiration date.

Question 9:

What is matrix parameter?

Matrix parameters are request URI parameters that take the form ;name=value. They are similar in purpose to query parameters but unlike query parameters can appear anywhere in the path and don't necessarily have to be at the end of the URI.

Below figure shows how to create a subresource with a matrix resource parameter in the request URI:

Question 10:

What all kind of output formats can be generated using RESTful web service?

The currently-available response formats for all REST endpoints are string-based formats and include XML, JSON, PJSON, and HTML.

Share interview questions


comments powered by Disqus


Social Media