REST (Representational State Transfer) is one of the most popular architectural styles for designing web services. RESTful APIs enable communication between client and server applications using HTTP protocols. Java, being a versatile programming language, is widely used for developing RESTful APIs with frameworks like Spring Boot, Jersey, and JAX-RS.
In this article, we will discuss some commonly asked Java REST API interview questions that can help you prepare for interviews. These questions cover basic to advanced concepts and are designed for both beginners and experienced developers.
Basic Interview Questions
1. What is REST? Explain its key principles.
Answer: REST (Representational State Transfer) is an architectural style for designing web services that use HTTP for communication. RESTful APIs are stateless and adhere to the following principles:
- Statelessness: Each request from the client must contain all the information needed by the server to fulfill it.
- Client-Server Architecture: The client and server are independent, allowing them to evolve separately.
- Uniform Interface: Consistency in the structure of API endpoints (e.g., using HTTP methods like GET, POST, PUT, DELETE).
- Cacheability: Responses can be cached to improve performance.
- Layered System: API architecture can have multiple layers (e.g., load balancers, authentication servers).
2. What is the difference between REST and SOAP?
Answer:
Aspect | REST | SOAP |
---|---|---|
Protocol | Works over HTTP/HTTPS | Can work over multiple protocols (HTTP, SMTP, etc.). |
Data Format | Supports multiple formats (JSON, XML, etc.) | XML-only. |
Statelessness | Stateless | Can be stateful or stateless. |
Complexity | Simpler and faster | More complex due to rigid standards. |
3. What are HTTP methods used in RESTful APIs?
Answer:
- GET: Retrieve data from the server.
- POST: Send data to the server to create a new resource.
- PUT: Update an existing resource.
- DELETE: Remove a resource.
- PATCH: Partially update a resource.
4. What is the difference between PUT and POST?
Answer:
- PUT: Used to update a resource or create it if it doesn’t exist. It is idempotent (multiple requests result in the same outcome).
- POST: Used to create a new resource. It is not idempotent (multiple requests may create multiple resources).
Intermediate Interview Questions
5. What are some common HTTP status codes in REST APIs?
Answer:
- 200 OK: The request was successful.
- 201 Created: A new resource was successfully created.
- 400 Bad Request: The server could not understand the request due to invalid syntax.
- 401 Unauthorized: Authentication is required.
- 403 Forbidden: The client is not allowed to access the resource.
- 404 Not Found: The requested resource was not found.
- 500 Internal Server Error: The server encountered an unexpected condition.
6. What is JAX-RS?
Answer:
JAX-RS (Java API for RESTful Web Services) is a specification in Java for creating RESTful web services. It provides annotations such as @GET
, @POST
, @Path
, and @Produces
to simplify the development of REST APIs.
7. What are the annotations provided by JAX-RS?
Answer:
@Path
: Defines the URL path for a resource or method.@GET
,@POST
,@PUT
,@DELETE
: Specify the HTTP methods for a resource.@Produces
: Specifies the response content type (e.g.,application/json
).@Consumes
: Specifies the request content type (e.g.,application/json
).@QueryParam
: Extracts query parameters from the URL.@PathParam
: Extracts parameters from the URL path.@HeaderParam
: Extracts values from HTTP headers.
8. Explain the difference between @RestController and @Controller in Spring.
Answer:
@Controller
: Used for building web applications that return views (e.g., JSP, Thymeleaf).@RestController
: A specialization of@Controller
that combines@Controller
and@ResponseBody
. It is used for RESTful APIs and returns JSON or XML responses directly.
9. What is the role of @RequestMapping
in Spring REST?
Answer:
@RequestMapping
is used to map HTTP requests to handler methods in Spring. It can define:
- HTTP methods (
method = RequestMethod.GET
) - URL patterns (
value = "/users"
) - Content types (
consumes = "application/json"
,produces = "application/json"
).
10. What is HATEOAS?
Answer:
HATEOAS (Hypermedia as the Engine of Application State) is a constraint of REST that allows clients to navigate through API resources using links provided in the response.
Example:
{
"id": 1,
"name": "John Doe",
"links": [
{
"rel": "self",
"href": "/users/1"
},
{
"rel": "orders",
"href": "/users/1/orders"
}
]
}
Advanced Interview Questions
11. How do you secure a REST API?
Answer:
- Authentication: Use OAuth2, JWT (JSON Web Tokens), or Basic Authentication.
- Authorization: Implement role-based access control (RBAC).
- HTTPS: Encrypt data in transit.
- Validation: Validate all inputs to prevent injection attacks.
- Rate Limiting: Limit API calls to prevent abuse.
- CORS: Configure Cross-Origin Resource Sharing to control access from other domains.
12. What is the difference between Monolithic and Microservices architecture?
Answer:
- Monolithic: The entire application is built as a single unit. Changes affect the whole system.
- Microservices: The application is divided into small, independent services that can be developed, deployed, and scaled separately. REST APIs are commonly used for communication between microservices.
13. What is Swagger, and how is it used?
Answer:
Swagger is a tool for designing, documenting, and testing RESTful APIs. It provides an interactive interface where developers can understand and test API endpoints. Swagger specifications are defined in YAML or JSON.
Java REST API development is a critical skill for modern software engineers. By understanding basic concepts, such as HTTP methods and status codes, to advanced topics like security and HATEOAS, developers can confidently design and implement robust RESTful services. Preparing for these interview questions will help you showcase your knowledge and expertise in Java REST APIs during interviews.