Rest-API Interview Questions – 1

Q. What is the REST API? Why is REST stateless

  • A REST API (Representational State Transfer Application Programming Interface) is a web service that follows REST architectural principles to allow clients to interact with resources (data objects) over HTTP using standard methods like GET, POST, PUT, DELETE.
  • REST is stateless, resource-based, and uses URLs to represent resources and HTTP methods to perform operations.
  • REST is called stateless because each HTTP request from a client to a server must contain all the information the server needs to understand and process that request.
  • The server does not store any context or session information between requests from the same client.
  • No server-side session:
    • The server doesn’t remember if you’ve logged in, what you did previously, or what your last request was.
  • Each request is independent:
    • Every request is a standalone interaction. It must include all necessary data (e.g., authentication token, resource identifier).

Q. What are the HTTP methods?

  • GET: Retrieves data from the server. It is safe and idempotent (does not change the resource). Used for reading data.
  • POST: Submits data to the server to create a new resource. Not idempotent—multiple identical requests may create multiple resources.
  • PUT: Updates an existing resource or creates it if it doesn’t exist. It is idempotent—subsequent identical requests have the same effect.
  • PATCH: Partially updates a resource. Not necessarily idempotent, but often treated as such.
  • DELETE: Deletes the specified resource. It is idempotent—deleting the same resource multiple times has the same result.

Q. Difference between PUT VS PATCH?
In HTTP, PUT replaces an entire resource, while PATCH updates specific fields within a resource, making PATCH a more efficient choice for partial updates. Here’s a more detailed explanation:

PUT:

  • Used to replace an entire resource with a new representation.
  • The client sends the complete data for the resource in the request body.
  • If the resource doesn’t exist, PUT can create it.
  • Example: Updating a user’s profile where you send all the user details, even if only one field is changed.

PATCH:

  • Used to apply partial modifications to a resource.
  • The client sends only the fields that need to be updated in the request body.
  • Example: Updating only a user’s address without changing other profile details.

Q. What is content negotiation

  • Content negotiation in REST APIs allows clients to specify the desired format of the response, such as JSON or XML, through HTTP headers. This enables the server to return the representation of a resource in a format the client can best understand.
  • It is a core principle of REST that enhances interoperability, flexibility, and client adaptability by not locking the API to a single response format.
  • Types of Content Negotiation:
    • Header-Based (most common): Uses the Accept header.
    • URL Extension-Based
    • Query Parameter-Based

Q. How does Spring Boot handle JSON and XML using the same API?

  • In Spring Boot, a single REST API endpoint can automatically handle requests with different Accept headers (e.g., application/json or application/xml) using content negotiation, without writing separate controller methods.
  • Spring Boot handles content negotiation automatically using HttpMessageConverters. JSON is supported out of the box through spring-boot-starter-web (via Jackson) by default. 
  • To add XML support, you include the spring-boot-starter-xml dependency and annotate your model class with @XmlRootElement.
  • Step-by-Step Setup in Spring Boot:
    • Add Dependencies: For XML support, you need to add JAXB (spring-boot-starter-xml).
    • Create a Model Class with JAXB Annotations (for XML):
      • You do not need to create a separate model for XML in Spring Boot. You can use the same model class for both JSON and XML—just add a JAXB annotation to make it XML-compatible.
    • REST Controller (nothing else needs to be provided).

Q. What is the Context Path in SpringBoot?
Note: If the interviewer is not asking this question, that means he/she is not worked on microservices.

  • In Spring Boot, the context path is the prefix of the URL under which your entire application is mapped. It’s essentially the base URI for all your REST endpoints.
  • Why is it Useful?
    • Deploy multiple services on the same server (e.g., /orders, /inventory).
    • Create cleaner routing under API gateways (e.g., WSO2, NGINX).
    • Improve the separation of concerns for monitoring, logging, and security.
    • Avoid endpoint collisions in monorepos or multi-module apps.

For the second Part: Rest-API Interview Questions – 2

Leave a Reply

Your email address will not be published. Required fields are marked *