Intermediate-Level REST API Interview Questions
11. What are the critical characteristics of REST?
The critical characteristics of REST are,
- We cannot start the server between two calls in a well-defined REST API.
- The REST uses the GET method to access the resources, whereas Web Services use the POST method to make operations.
- REST is stateless. Stateless means that the REST API server has no session data.
12. What markup languages can be used in a RESTful web API?
JSON(JavaScript Object Notation) and XML(Extensible Markup Language) are the two languages that are used for creating a RESTful web API. Markup languages are often not used directly to represent data in a RESTful web API. Instead, structured formats like XML and JSON (JavaScript Object Notation) are used often to exchange data. These formats are suitable for data serialization and deserialization in HTTP requests and responses because they are small and easily accessible by humans.
13. How is JAXB related to RESTful web API?
In terms of data serialization and deserialization, JAXB (Java Architecture for XML Binding) and RESTful web APIs are connected. XML documents can be mapped to Java objects and vice versa using JAXB, a Java technology. Although JAXB is not specifically REST, it can be used with RESTful web services to process XML data.
14. What are the tools used for testing and developing web API?
We can use the following tools for testing a RESTful web API,
- Spring REST
- Axis
- CXF
- Jersey API
- Restlet
15. What is SOAP?
SOAP (Simple Object Access mechanism) is a mechanism is used to exchange structured data while creating web services. It is renowned for its tight standards and robust typing and employs XML as its communication format, making it appropriate for enterprise-level applications. Through the use of SOAP, several systems can communicate with one another over a network in order to call methods and exchange data in a platform- and language-neutral fashion.
16. What are JAX-WS and JAX-RS?
Java technologies for creating web services include JAX-WS (Java API for XML Web Services) and JAX-RS (Java API for RESTful Web Services):
JAX-WS: It concentrates on building web services that are SOAP-based and enables Java applications to communicate via the SOAP protocol.
JAX-RS: With the use of HTTP methods like GET, POST, PUT, and DELETE, Java programs may construct and use RESTful web services.
17. What is the architectural style for creating a web API?
The architectural style for creating a web API is,
- HTTP is used for the client and server communication
- XML or JSON is used as the formatting or markup language
- Simple URI are preferred as the address for the web services
- The server has a stateless communication
18. What are the features of a RESTful web API?
The features of using a RESTful web API are,
- The RESTful APIs are based on the client-server model
- REST uses HTTP(HyperText Transfer Protocol) for the communication
- Resources can be easily accessed with the help of URI
- REST follows the concept of a stateless server
- REST can use a cache to minimize the number of server calls
19. What is the statelessness of a server?
Statelessness of a server, often known as "statelessness" in the context of web applications and RESTful services, refers to the server's inability to retain any context or data on past interactions or requests from clients. Each client request is handled independently, and the server doesn't keep track of previous requests in its memory.
20. What are the most commonly used status codes in a web API?
The most common status codes in a web API are,
- 200 - Success
- 201 - Created
- 304 - Not Modified
- 400 - Bad Request
- 401 - Not Authorized
- 404 - Not Found
- 500 - Integral Server Error
- 502 - Bad Gateway
REST API Experienced Interview Questions
21. What are the disadvantages of using RESTful web API?
The disadvantages of using RESTful web services are,
- Since the RESTful web services follow statelessness, it is almost impossible to maintain sessions.
- We cannot impose security restrictions in REST. REST can only inherit the security restrictions imposed by the protocols implemented.
22. What are some of the best practices while developing a RESTful web API?
Some of the best practices to follow while developing a RESTful web API are,
- It is always recommended to use JSON data format as much as possible while creating a RESTful web API.
- The user should name the resource endpoints as plural nouns and not verbs.
- The user should return appropriate error messages in case an error is encountered.
- If the user is working with huge resource data, it is recommended to use the concepts of filtering and pagination.
- The user is advised to use the cache feature provided by REST to enhance the user experience.
- The user should ensure good security practices to make the API secure.
23. What are Idempotent methods?
The Idempotent methods ensure that if a single request is called multiple times, the response should be the same for all the calls. While developing a REST API, it is always recommended to make the API Idempotent to ensure consistency in the API. They are HTTP methods that, when called repeatedly with the identical input parameters, yield the same outcome. In other words, repeated idempotent operations should have the same result as a single idempotent operation.
24. What are the core components of an HTTP Request?
There are five main components of any HTTP Request,
- Method: The developer needs the action to be performed. The developer can choose any of the methods, for example, GET, POST, PUT, etc.
- URI: This part is used to identify the resource on the server.
- HTTP Version: This part is used to specify the HTTP protocol version used.
- Request Header: This part contains all the required metadata for the request message.
- Request Body: This part contains the message to be passed to the server.
25. What are the core components of an HTTP Response?
There are four main components of any HTTP Response,
- Response Status Code: This part contains the status code for the requested server.
- HTTP Version: This part is used to specify the HTTP protocol version used.
- Response Header: This part contains all the required metadata for the response message.
- Response Body: This part contains the message to be passed to the client.
26. Define RestTemplate in Spring.
The basic class providing client-side access to Spring-based RESTful services is RestTemplate. The REST restrictions are used to communicate with the server. This is similar to other template classes offered by Spring, such as JdbcTemplate and HibernateTemplate. The RestTemplate provides high-level implementation details for HTTP Methods such as GET, POST, PUT, and others and allows them to communicate using the URI template, URI path params, request/response types, request object, and so on as arguments.
27. What is HTTP Basic Authentication?
When using Basic Authentication as part of APIs, the user must submit the username and password, which are then concatenated by the browser in the form "username: password" and then base64 encoded. On every HTTP request from the browser, the encoded value is delivered as the value for the "Authorization" header. Since the credentials are just encoded, it is recommended to use this form when sending HTTPS requests because they are not secure and can be intercepted by anybody if security protocols are not utilized.
28. How are REST APIs kept secure?
In URIs, sensitive information such as a user id, password, or verification token should not be displayed. REST APIs can be kept secure by using security measures such as authorization and authorization, API server confirmation, TLs/SSL encryption, rate-limiting for DDoS assaults, and others.
29. Describe the payload.
The term "payload" in GET or POST requests refers to data in the HTTP response and/or response messages. For example, if you ask the Twitter API for a specific tweet, the payload will include the document describing the tweet's text as well as any files required to embed the tweet on a website. The POST method can also be used to include the payload of an HTTP request. When utilizing Twitter's API, the payload is the tweet text that you provide in the POST request.
30. How are APIs tested?
JMeter, Katalon, and PostmanStudio are among the software tools that can be used to test RESTful APIs. Sending several calls from the testing tool and observing how your API responds are frequent tasks in the testing process. Because of the automated testing capabilities provided by various testing solutions, you may easily execute a variety of scenarios.
JAX-RS Interview Questions
31. What is JAX-RS?
JAX-RS stands for Java API for RESTful Web Services. It is a Java programming language API specification. It supports building RESTful web services. It allows developers to create HTTP-based web services that follow the REST architectural style.
32. What are the main annotations used in JAX-RS?
The main annotations used in JAX-RS include @Path, @GET, @POST, @PUT, @DELETE, @Produces, @Consumes, and @QueryParam. These annotations define the URI paths, HTTP methods, and the format of input/output data for the web services.
33. What are the main components of JAX-RS?
The main components of JAX-RS are:
- Resource Classes: These are Java classes that represent the resources the web service exposes.
- Resource Methods: These are the methods within the resource classes that handle the HTTP requests and responses.
- URI Templates: These are used to map the URL paths to the resource methods.
- Providers: These classes handle the serialization and deserialization of data, exception handling, and other cross-cutting concerns.
34. What is the purpose of the @Path annotation in JAX-RS?
The @Path annotation is used to specify the URI path that a particular resource or method will respond to. It is used at both the class level and the method level to define the hierarchical structure of the RESTful service.
35. What is the difference between @Path and @PathParam annotations?
The @Path annotation is used to specify the URI path to the resource class or method. The @PathParam annotation is used to bind a method parameter to a part of the URI path.
For example, in the URI path /customers/{id}, the {id} part is the path parameter, and it can be bound to a method parameter using the @PathParam annotation.
36. What is the difference between JAX-RS and Servlets?
JAX-RS and Servlets are both Java technologies for building web applications, but they have some key differences:
- Architectural Style: JAX-RS is designed to build RESTful web services, while Servlets are more generic and can be used for any type of web application.
- Abstraction Level: JAX-RS provides a higher-level abstraction for handling HTTP requests and responses, while Servlets require more manual handling of the low-level details.
- Annotations: JAX-RS heavily uses annotations to configure the behavior of the web service, while Servlets rely more on configuration files.
- Serialization/Deserialization: JAX-RS provides built-in support for serializing and deserializing data, while Servlets require more manual handling of this process.
37. How do you handle exceptions in JAX-RS?
In JAX-RS, exceptions can be handled using @Provider and ExceptionMapper interfaces. By implementing ExceptionMapper, you can map exceptions to HTTP responses.
38. What is @Consumes annotation in JAX-RS?
The @Consumes annotation specifies the media types (like JSON, XML) that a resource method can accept as input. It is typically used with methods that process HTTP POST or PUT requests.
39. What is the difference between Jersey and RESTEasy?
Jersey and RESTEasy are two popular implementations of the JAX-RS specification. While they share many similarities, there are some differences:
- Specification Compliance: Jersey is the reference implementation of the JAX-RS specification, while RESTEasy is a separate implementation that aims to be compatible with the specification.
- Feature Set: Jersey has a more comprehensive set of features and integrations, while RESTEasy has a more focused and lightweight approach.
- Performance: RESTEasy is generally considered to have better performance and lower overhead than Jersey.
- Community and Ecosystem: Jersey has a larger community and more third-party integrations, while RESTEasy has a more focused and dedicated community.
40. How do you handle CORS in JAX-RS?
CORS (Cross-Origin Resource Sharing) can be handled in JAX-RS by using @Provider annotation along with a ContainerResponseFilter implementation that adds the necessary CORS headers to the HTTP response.
Spring RESTful Web Services Interview Questions
41. What is Spring REST?
Spring REST is a part of the Spring Framework that allows you to create RESTful web services in a Spring-based application. It uses annotations like @RestController, @RequestMapping, and others to define RESTful endpoints.
42. What are the key features of Spring REST?
Some of the key features of Spring REST include:
- Annotations-based Configuration: Spring REST heavily uses annotations like @RestController, @RequestMapping, and @ResponseBody to configure the web service.
- Automatic Response Conversion: Spring REST can automatically convert Java objects to the appropriate HTTP response format (e.g., JSON, XML) based on the request's Accept header.
- Exception Handling: Spring REST provides a robust exception handling mechanism, allowing you to map exceptions to appropriate HTTP status codes.
- Security Integration: Spring REST integrates seamlessly with the Spring Security framework, making it easy to secure your RESTful APIs.
- Testing Support: Spring REST provides dedicated testing utilities and annotations to simplify the process of testing your RESTful web services.
43. How do you handle exceptions in Spring RESTful web services?
In Spring RESTful web services, exceptions can be handled using @ControllerAdvice and @ExceptionHandler annotations, which allow you to define global or local exception-handling logic.
44. What is the purpose of the @RequestMapping annotation?
The @RequestMapping annotation is used to map HTTP requests to handler methods in a Spring REST application. It can be applied at the class level to define the base URL path, and at the method level to define the specific HTTP method and URL path that the method should handle.
For example, @RequestMapping(value = "/buyers", method = RequestMethod.GET) would map the HTTP GET request to the /buyers URL path to the annotated method.
45. How can you consume and produce JSON in Spring RESTful services?
To consume and produce JSON in Spring RESTful services, you use the @RequestBody and @ResponseBody annotations along with MediaType.APPLICATION_JSON_VALUE.
46. What is the purpose of the @RequestBody annotation?
The @RequestBody annotation is used to bind an HTTP request body to a method parameter. This is useful when you need to accept a request payload (e.g., JSON, XML) and map it to a Java object.
For example, a method with the signature createCustomer(@RequestBody Customer customer) would automatically deserialize the request body into a Customer object.
47. How do you secure Spring RESTful web services?
Spring RESTful web services can be secured using Spring Security, which provides features like authentication, authorization, and protection against CSRF attacks.
48. What is the purpose of the @JsonIgnore and @JsonProperty annotations?
The @JsonIgnore annotation is used to mark a field or method in a Java class that should be ignored during the JSON serialization/deserialization process.
The @JsonProperty annotation is used to specify the name of the JSON property that should be used to map a field or method in a Java class during the JSON serialization/deserialization process.
49. How do you handle pagination in Spring RESTful web services?
Pagination in Spring RESTful web services can be handled by passing page and size parameters to the API and using them in the repository queries. You can also return a Page object from the service layer.
50. How can you implement versioning in Spring RESTful web services?
Versioning in Spring RESTful web services can be implemented using URI versioning, request parameter versioning, header versioning, or content negotiation.
REST API MCQ
1. What does REST stand for?
- A) Representational State Transfer
- B) Remote State Transfer
- C) Representational Stateful Transfer
- D) Remote Stateful Transfer
Answer: A) Representational State Transfer
2. Which HTTP method is idempotent in REST?
- A) GET
- B) POST
- C) PATCH
- D) DELETE
Answer: A) GET
3. What does @GET annotation represent in JAX-RS?
- A) It maps the method to a GET request
- B) It maps the method to a POST request
- C) It maps the method to a PUT request
- D) It maps the method to a DELETE request
Answer: A) It maps the method to a GET request
4. Which status code represents a successful HTTP request in REST?
Answer: C) 200
5. What is the main difference between SOAP and REST?
- A) REST is stateless, SOAP is stateful
- B) SOAP is stateless, REST is stateful
- C) SOAP uses only XML, REST can use XML, JSON, etc.
- D) REST uses only XML, SOAP can use XML, JSON, etc.
Answer: C) SOAP uses only XML, REST can use XML, JSON, etc.
6. Which of the following is not a REST constraint?
- A) Statelessness
- B) Client-Server Architecture
- C) Code on Demand
- D) SMTP
Answer: D) SMTP
7. What is @Produces used for in JAX-RS?
- A) To specify the HTTP method
- B) To specify the response media type
- C) To specify the request media type
- D) To specify the URI path
Answer: B) To specify the response media type
8. What does @RequestBody do in Spring REST?
- A) It maps the HTTP request body to a method parameter
- B) It maps the HTTP response body to a method parameter
- C) It maps the HTTP headers to a method parameter
- D) It maps the HTTP path variables to a method parameter
Answer: A) It maps the HTTP request body to a method parameter
9. Which status code indicates that the resource has been created successfully in REST?
Answer: B) 201
10. What is HATEOAS in RESTful web services?
- A) Hypertext Application Technology over XML
- B) Hypertext Application Transfer Protocol
- C) Hypermedia as the Engine of Application State
- D) Hypertext and Application Transfer Object State
Answer: C) Hypermedia as the Engine of Application State
Frequently Asked Questions
How do I prepare for REST API interview?
You must understand the fundamentals of REST APIs and their implementation methods. Aside from that, you must understand the concept of statelessness and the various HTTP status codes. It is one of the most common REST API interview questions.
How do you explain REST API in an interview?
When asked about a REST API in an interview, you should be able to describe it as a set of architectural guidelines and limitations for developing web services that permit interaction with resources on the internet.
What are the 3 components of a RESTful API?
The three main components of REST API are Uniform Resource Identifiers, Methods, and HTTP Headers. The URL specifies the path to the resource. An HTTP method tells the server what it needs to do to the resource. Headers are the metadata exchanged between the client and server.
What are REST APIs interview questions?
Some REST API interview questions include: what is REST API, what its features are, and what is addressed in RESTful web services. One of the most frequently asked questions is what the difference is between API and REST API.
What are the 3 principles for a RESTful API?
The three principles for a RESTful API are statelessness, meaning each request is independent; uniform interface, ensuring consistent resource access; and client-server separation, which decouples the client from server logic.
What are the 4 main benefits of using REST APIs?
The four main benefits of using REST APIs are simplicity in implementation, scalability to handle large amounts of data, flexibility to support multiple formats, and statelessness, which ensures improved performance and reliability.
Conclusion
In this blog, we have discussed the top 50+ REST API Interview Questions and Answers (2024). These interview questions are crucial for both beginners and experienced professionals preparing for job interviews in 2024. Understanding the core concepts, principles, and best practices of RESTful APIs is essential for building robust and scalable web services. As REST continues to be the backbone of modern web development, mastering these questions will give you a significant edge in interviews, helping you to demonstrate your knowledge and skills effectively.
Recommended Readings: