Following are some of the questions on REST with my understanding as answer:
---------------------------------------------------------------------------------------
REST Interview Question 1:
What is the Caching mechanism that a RESTful service would provide?
This is how I would answer this question
As RESTful service uses HTTP as transport protocol, it can leverage caching
features from HTTP specification as well.
HTTP 1.0 specification has Expires header that can be used to indicate ways
to client side for the appropriate caching intentions of server.
HTTP 1.1 specification however has more caching related features to choose from.
These are in form of various directives used along with header such as Cache-Control.
---------------------------------------------------------------------------------------
REST interview question 2:
What are those comma separated directives of Cache-Control header?
This is how I would answer this question
The comma separated directives of Cache-Control headers are
private, public, no-cache, no-store, no-transform, max-age, s-maxage.
---------------------------------------------------------------------------------------
REST interview question 3:What are the differences between no-cache, and no-store directives used along with
Cache-Control header?This is how I would answer this question
no-cache 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.
While no-store 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.
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.
---------------------------------------------------------------------------------------
REST interview question 4:If any intermediary proxy is not used to server any response to client's request,
is there any difference between private and public directives of Cache-Control?This is how I would answer this question
No, as private directive may be used to restrict cahing at proxy/CDN server that could
be some intermediary destinations while serving response.
---------------------------------------------------------------------------------------
REST interview question 5:What is the difference in usage for the s-maxage and max-age directives?This is how I would answer this question
s-maxage may be used for the proxy/CDN server to know that this is the directive
for as maximum age for the data that is sent as response.
While max-age is used as a directive to the client as the maximum age for the data
that is sent along with response.
---------------------------------------------------------------------------------------
REST interview question 6:What are the various annotations available from JAX-RS api specification,
for sending data from client-side to service endpoint?This is how I would answer this question
JAX-RS has provided various annotations for passing data from client-side code
to service are @PathParam, @FormParam, @MatrixParam, @QueryParam, @HeaderParam, @CookieParam.
These annotations are also known as Injection Annotations from JAX-RS API.
---------------------------------------------------------------------------------------
REST interview question 7:How to inject Web container related values and configurations to the JAX-RS service
implementation instanceThis is how I would answer this question
There is an annotation @Context provided by JAX-RS specification for the REST service
Implemntation to be able to receive helper and web container specific configuration values.
---------------------------------------------------------------------------------------
REST interview question 8:Can you write-down an example of MatrixParam expression?This is how I would answer this question
For example, for the following URI:
/employees/name=Ishtek;age=34
@MatrixParam("age") would return 34 as value, as matrix param precedes with a ';' as
separator.
---------------------------------------------------------------------------------------
Are you aware of @BeanParam annotation?Auther's View point/Answer to above question:
Yes, @BeanParam annotation is added in JAX-RS 2.0 version. This annotation can be used
along with a Bean class for using other annotation types such as @FormParam, @HeaderParam etc.
as the field level, for using an application specific bean class as argument in the service
method, rather than using a long list of argument parameters for each of the different type of
attributes used along with a request.
---------------------------------------------------------------------------------------
Can you elaborate on usage of @BeanParam with an example?Auther's answer to above question:
Suppose there is a HTML form with ten fields/attributes that is used to receive input from user,
and this data/fields are to be submitted to the service method, then the REST service method would
require to define all the ten attributes as arguments for the service method along with @FormParam
annotation. Instead @BeanParam can be used to declare an application/user defined bean class with
all these ten attributes as fields. This user defined class can be a single argument to the REST
service method argument. This way there could be minimal impact when number of fields changes while
using POST as HTTP method.
---------------------------------------------------------------------------------------
How to approach for change in attributes to the service method in a Webservice ?Auther's view/answer :
In order to minimize change/impact on the client side of code, when there is a change in
argument parameters of the service method, one can choose to use user defined bean class
as argument to service method, rather than using all the arguments directly in the service
method definition. In this way, if there is a need for addition or removal of any attribute/argument
from the service method, no change to the service method definition would be needed.
---------------------------------------------------------------------------------------
What are the major differences you can state while using SOAP or REST, in terms of
applicability as concern?
Some of the differences that may be observed in applicability of SOAP or REST as the
service language/specification :
1. When requirement is to provide a business process as a service, then SOAP may get
little more attention than RESTful services.
2. When we are exposing a server side object as many different type of representations
for the client, such as JSON, TXT, XML, Audio, Video and many more (HTTP content types)
etc. RESTful services can be used/more appropriate than SOAP.
3. In case of contract/interface based service definitions are to be used, then SOAP can be used.
4. In case of exposing a service for any type of devices, be it Desktop/Laptop/Netbook, Tablet,
Mobile phones, Kindle etc., and consumer can be a browser (Thin client) or a native application
(Thick client). In this circumstances we can opt for RESTful services.
5. In case of many different types of transports are to be used for using a service, then SOAP would
be appropriate over RESTful service.
6. For looking for standards-based service declarations and usage, SOAP has many standards to use,
such as WS-* standards. Whereas RESTful services would be a specification way of exposing and
using any service.
7. Looking at slightly more technical aspects of SOAP, SOAP supports custom objects definitions using
XML Schema and marshalling/unmashalling of various datatypes to communicate across diverse platforms.
---------------------------------------------------------------------------------------
How can you apply security to RESTful services
Some of the options available to use for securing a RESTful service, for now, are
1. Basic Authentication
This type of Authentication will require transport level encryption(SSL), as user
credentials are to be sent via wire in plain text.
2. OAuth 1.0a / OAuth 2.0
OAuth 1.0a is using advanced encryption for passing token for authentication purposes.
OAuth 2.0 is using SSL for transport level security.
3. Custom/Third-party security protocol
---------------------------------------------------------------------------------------
What is the main factor to consider while choosing OAuth version to use, whether to use
OAuth 1.0a or OAuth 2.0?
The main reson is the sensitivity of the data that is exchanged, and transport level
security related considerations. If the application data is less sensitive,
the OAuth 1.0a could be well enough for use, and OAuth 1.0a specification can be applied
without much of encryptions on transport. But OAuth 2.0 would rely on HTTPS transport
level security/encryption for communication.
---------------------------------------------------------------------------------------
What are the various credential types used along with OAuth 2.0?
There are three types of credentials available to use along with OAuth 2.0, such as
Bearer Token, MAC token, SAML.
---------------------------------------------------------------------------------------
What are the HTTP methods corresponding to CRUD operations?
POST - Create
GET - Read
PUT - Update
DELETE - Delete
are the corresponding HTTP method used for CRUD operations with resource(s).
---------------------------------------------------------------------------------------
Can you write a very simple code showing resource being exposed as RESTful service?
@Path("/book")
public class Book {
@GET
@Path("{id}")
public Book getBookInfo(@PathParam("id") String bookId) {
//return Book Instance by using value bookId.
return new Book();
}
}
Acessing this Book resource by using an URI as <>/book/b001
---------------------------------------------------------------------------------------
What are the annotations that can be used for specifying content-type that is supported
by any RESTful service?
@Produces("text/xml") and @Consumes("text/xml") are the annotations that are used for specifying
ways of defining any restrictions that can be defined at method-level for any RESTful service.
---------------------------------------------------------------------------------------
For using JAXB supported XML to Custom-object and Custom-object to XML mapping/conversion,
along with RESTful service, what are the annotations those can be used along with custom object?
@XmlRootElement, @XmlElement, @XmlAttribute, @XmlAccessorType etc., are the annotations from JAXB
can be used along with custom class for defining class, field level fields.
---------------------------------------------------------------------------------------
As @FormParam can be used for passing form parameters in request, but in case parameter
set is likely to change (parameters can be added or removed), then how to insulate RESTful service
method definition from change?
MultivaluedMap type can be used to define argument parameter for the RESTful method
signature for passing key and value pair in request.
---------------------------------------------------------------------------------------
Is there any ways to provide custom/own JAXBContext for marshalling/unmarshalling XML to Object
and vice versa, rather than using default JAXBContext as available with JAX-RS provider?
Yes, by implementing class file that implements ContextResolver and overriddes
public getContext (Class) method for returning custom implementation of JAXBContext.
Added on this page, as of 03-July-2014:Have you used Jersey framework or any other implementation for JAX-RS specification?Answer :
Jersey Framework can be used along with web container that is having support for JAX-RS
or not. Where as any web container that has support for JAX-RS specification, can be used
to provide a resource as RESTful web service.
---------------------------------------------------------------------------------------
Is there any ways to code so as to provide HTTP headers to a method in the main RESOURCE class
file?Answer :
Yes, by using @Context as argument type for the method that is exposed as REST uri.
import javax.ws.rs.core.HttpHeaders;
...
...
@GET
@Path("abc")
public void getValue(@Context HttpHeaders headers) {
...
...
}
---------------------------------------------------------------------------------------
Have you used Maven to generate Jersey based RESTful services? if yes, how?Answer :
I have used a archetype from Maven repository, called as 'jersey-quickstart-webapp',
for generating a web application, just to start head's up on using Jersey for creating
RESTful services.
---------------------------------------------------------------------------------------
Can you be able to provide certain context-level parameters in web.xml, and receiving
parameter value in the resource method?Answer :
One can define context parameter in web.xml file and corresponding value of the parameter
in the resource method, by using @Context annotations with instance level variable of
ServletContext type.
---------------------------------------------------------------------------------------
What are the headers types from HTTP request, those can be used for mapping same URI
but different resource methods? Answer :
One can use Accept, Accept-Language, Accept-Encoding, Content-Type with appropriate values
for mapping same URI but different methods. Appropriate method would be called that is
receiving corresponding values in form of those headers, in the HTTP REQUEST.
Please keep visiting this page... as more questions will be added here,
as and when available.
---------------------------------------------------------------------------------------
Is that any ways to approach so that changes(addition/deletion) in the form-level parameters
would not have impact on the Resource method signature/argumentsAnswer :
One can explore javax.ws.rs.core.MultivaluedMap