Java API for RESTful Web Services (JAX-RS) - Interview Questions

What do you understand by REST?

 

REST, which stands for 'Representational State Transfer', is a client-server architecture style of transferring representations of resources through requests and responses.

Following are the key attributes of REST architectural style.

1. Data and functionality are considered as REST resources.

2. REST Resources are accessed using URIs (Uniform Resource Identifiers), which are usually web links.

3. REST resources are represented by documents. Example - XML document, JSON document, HTML page, Image file etc.

4. REST Resources can be retrieved and updated using simple well-defined operations.

What are RESTful web services?

 FAQ

RESTful web services are simple, lightweight and loosely coupled web services used for creating APIs to REST resources, and are well suited for clients on the internet.

Following are the key principles of RESTful web services which make them lightweight and fast.

1. Resource access through URIs - A RESTful web service exposes a set of REST resources through URIs which are standard links on the Internet.

2. Stateless - RESTful web services are stateless and designed to be used with a stateless protocol, usually HTTP.

3. Uniform interface - A RESTful web service provides a standard set of four operations to create, read, update and delete resources - GET, PUT, POST and DELETE. These correspond to the GET, PUT, POST and DELETE methods of the HTTP protocol.

4. Multiple document formats - Resources are decoupled from their representation. So the same content can be accessed in a variety of formats such as XML, JSON, PDF, JPEG etc.

Describe the key features of JAX-RS API that is provided in Java EE?

 FAQ

JAX-RS is a set of Java APIs, provided in Java EE, that are used for the development of RESTful web services.

Following are the key features of the JAX-RS API.

1. POJO-based API - The JAX-RS API provides a set of annotations and corresponding classes and interfaces that can be used with POJOs to expose them as web services.

2. HTTP-based API - The JAX-RS API is designed for HTTP as the base protocol. The API supports HTTP usage patters and provides mapping between HTTP actions and corresponding API classes and annotations.

3. Format Independent - The JAX-RS API is applicable to a wide variety of HTTP entity body content types.

4. Container Independent - JAX-RS applications can be deployed in a Java EE container, Servlet container or can be plugged in as a JAX-WS provider.

What are the key annotations provided in the JAX-RS API?

 FAQ

The JAX-RS API provides a set of annotations and its corresponding classes and interfaces that can be used on POJOs to expose them as RESTful web services.

Following are some key annotations defined in the JAX-RS API

@Path - @path annotation defines the relative URI path for the RESTful resource.

@GET - The @GET annotation is a request method designator. A Java method that is designated with this annotation will process HTTP GET requests.

@POST - The @POST annotation is a request method designator. A Java method that is designated with this annotation will process HTTP POST requests.

@PUT - The @PUT annotation is a request method designator. A Java method that is designated with this annotation will process HTTP PUT requests.

@DELETE - The @DELETE annotation is a request method designator. A Java method that is designated with this annotation will process HTTP DELETE requests.

@HEAD - The @HEAD annotation is a request method designator. A Java method that is designated with this annotation will process HTTP HEAD requests.

@PathParam - The @PathParam annotation is the URI path parameter that you can extract and use in your resource class.

@QueryParam - The @QueryParam annotation is the URI query parameters that you can extract and use in your resource class.

What are RESTful Root Resource Classes in JAX-RS API?

 FAQ

Root Resource classes are POJOs (Plain Old Java Objects) that are either annotated with @Path - or have at least one method annotated with @Path or a request method designator such as @GET, @POST, @PUT, @DELETE etc.

import javax.ws.rs.Path;

/**
* Root resource (exposed at 'service' path)
*/
@Path('service')
public class OrderService { ... }

What are request method designator annotations in JAX-RS API?

 FAQ

Request method designator annotations are the runtime annotations @GET, @PUT, @POST, @DELETE and @HEAD, defined in the JAX-RS API, that can be applied to Java methods. Request method designator annotations correspond to the corresponding HTTP request methods GET, PUT, POST, DELETE and HEAD.

import javax.ws.rs.Path;

/**
* Root resource (exposed at 'myservice' path)
*/
@Path('myservice')
public class OrderService {

 @GET
 public String getOrder() {...} 
}

What is the role of @path annotation in JAX-RS API?

 FAQ

The @Path annotation identifies the URI path template to which the resource responds and is specified at the class or method level of a resource.

URI path templates are URIs with variables embedded within the URI text.

import javax.ws.rs.Path;

/**
* Root resource (exposed at '/orders/' path)
*/
@Path('/orders/{orderNumber}')
public class OrderService {

 @GET
 public String getOrder() {...} 
}

What is the role of @Produces annotation in JAX-RS API?

 FAQ

The @Produces annotation specifies the MIME media types or representations that a resource can produce and send back to the client.

If @Produces is applied at the class level, all the methods in a resource can produce the specified MIME types by default. If applied at the method level, the annotation overrides any @Produces annotations applied at the class level.

If no methods in a resource are able to produce the MIME type in a client request, the JAX-RS runtime sends back an HTTP '406 Not Acceptable' error.

import javax.ws.rs.Path;

/**
* Root resource (exposed at '/orders/' path)
*/
@Path('/orders/{orderNumber}')
public class OrderService {

 @GET
 @Produces('text/xml')
 public String getOrder() {...} 
}

What is the role of @Consumes annotation in JAX-RS API?

 FAQ

The @Consumes annotation is used to specify which MIME media types of representations a resource can accept, or consume, from the client. If @Consumes is applied at the class level, all the response methods accept the specified MIME types by default. If applied at the method level, @Consumes overrides any @Consumes annotations applied at the class level.

If a resource is unable to consume the MIME type of a client request, the JAX-RS runtime sends back an HTTP 415 ('Unsupported Media Type') error.

The value of @Consumes is an array of String of acceptable MIME types, or a comma-separated list of MediaType constants.

What are the key steps to create a client request using JAX-RS API?

 FAQ

The JAX-RS API provides a high-level API for accessing JAX-RS services as well as other REST services. The JAX-RS Client API is defined in the javax.ws.rs.client package.

Following are the key steps to create a client request.

1. Obtain an instance of the javax.ws.rs.client.Client interface. The client instance can be created calling newClient() method javax.ws.rs.client.Client class.

2. Configure the client with the target URI to call. This is done by calling the target() method on the client instance and passing the target URI as a parameter.

3. Create a request based on the target URI and set the required attributes such as media type.

4. Call the request by invoking one of the request methods on the target service.

How do you set message headers in client request using JAX-RS API?

 FAQ

You can set HTTP headers on the request by calling the header() method on the client target.

How do you set cookies in client request using JAX-RS API?

 FAQ

You can add HTTP cookies to the request by calling the cookie() method on the client target, which takes a name-value pair as parameters.

How do you set filters in client request or response using JAX-RS API?

 FAQ

You can register custom filters with the client request or the response received from the target resource. To register filter classes when the Client instance is created, call the Client.register method.

You can also register the filter classes on the target by calling WebTarget.register.

How do you make asynchronous client request calls using JAX-RS API?

 FAQ

In the JAX-RS Client API, the Invocation.Builder.async() method is used when constructing a client request to indicate that the call to the service should be performed asynchronously. An asynchronous invocation returns control to the caller immediately, with a return type of java.util.concurrent.Future and with the type set to the return type of the service call. Future objects have methods to check if the asynchronous call has been completed, to retrieve the final result, to cancel the invocation, and to check if the invocation has been canceled.

How do you configure JAX-RS applications?

 FAQ

A JAX-RS application consists of resource classes that are packaged within a war file. There are two ways to configure a JAX-RS application

1. By using the @ApplicationPath annotation in a subclass of javax.ws.rs.core.Application packaged within the WAR.

2. By using the servlet-mapping tag within the WAR's web.xml deployment descriptor.

 
Subscribe to our Questions

 

Java EE - Interview Questions

Java ServletsJavaServer PagesJavaServer FacesJava API for XML Processing (JAXP)Java API for JSON Processing (JSON-P)Java Message Service API (JMS)Java Persistance API (JPA)Java Transaction API (JTA)JAX-WSJava API for RESTful Web Services (JAX-RS)Java Database Connectivity API (JDBC)Security
 
RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example