Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

List the key annotations that are present in the JAX-RS API?

Answer»
  • @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.
2.

Is it possible to make asynchronous requests in JAX-RS?

Answer»

Yes. the JAX-RS Client API provides a method called Invocation.Builder.ASYNC() that is used for constructing client requests that need to be executed asynchronously. Invoking a request asynchronously does the task of returning the CONTROL to the caller by returning with datatype java.util.concurrent.Future whose type is set to return the service call type. Future objects are used because they have the required methods to check whether the asynchronous calls have been completed and if yes, then retrieve the responses. They also provide the flexibility to cancel the request invocations and also check if the cancellation has been successful.

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); // indicates whether the Future was cancelled or not boolean isCancelled(); // indicates if the executor has completed the task boolean ISDONE(); // gets the actual result from the process. // This blocks the program execution until the result is ready. V get() throws InterruptedException, ExecutionException; // also gets actual result from the process but it throws // the TimeoutException in case the result is not obtained before specified timeout V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;}

Let us consider we have this function below which is used for processing 2 Ids parallelly.

public void processIds(String userId1, String questionId){ Client client = ClientBuilder.newClient(); Future<Response> futureResponse1 = client.target("http://interviewbitserver.com/users/"+USERID).request().async().get(); Future<Order> futureResponse2 = client.target("http://interviewbitserver.com/questions/"+questionId).request().async().get(Question.class); // block the process until complete Response response1 = futureResponse1.get(); User userObject = response1.readEntity(User.class); //Do processing of userObject // Wait for 2 seconds before fetching record try { Question question = futureResponse2.get(2, TimeUnit.SECONDS); //Do Processing of question } catch (TimeoutException timeoutException ) { //handle exceptions } return;}

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.

3.

How can the JAX-RS applications be configured?

Answer»

JAX-RS APPLICATIONS have the root resource classes packaged in a WAR file. There are 2 means of configuring JAX-RS applications.

  1. Use @ApplicationPath annotation in a subclass of javax.ws.rs.core.Application that is packaged in the WAR file.
  2. Use the <servlet-mapping> TAG inside the web.xml of the WAR. web.xml is the deployment descriptor of the application where the mappings to the SERVLETS can be DEFINED.
4.

What do you understand by request method designator annotations?

Answer»

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' path*/@Path('resource_service')public class InterviewBitService { @GET public String getRESTQuestions() { // some operations } }
5.

Define RESTful Root Resource Classes in the JAX-RS API?

Answer»
  • A resource CLASS is nothing but a Java class that uses JAX-RS provided annotations for IMPLEMENTING web resources.
  • They are the POJOS that are annotated either with @Path or have at least one METHOD annotated with @Path, @GET, @POST, @DELETE, @PUT, etc.

Example:

import javax.ws.rs.Path;/*** InterviewBitService is a root resource class that is exposed at 'resource_service' path*/@Path('resource_service')public class InterviewBitService { // DEFINED methods}
6.

What are the key features provided by JAX-RS API in Java EE?

Answer»

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.