Modern applications rarely operate in isolation. Mobile apps, web frontends, and microservices constantly exchange data. This communication happens almost entirely through APIs, specifically using the REST (Representational State Transfer) architectural style.
Spring Boot makes building REST APIs incredibly elegant by utilizing highly specialized annotations. In this guide, we will break down the entire request lifecycle, internal mechanisms, and build a complete Employee Management API.
1. What is a REST API?
Standard Definition
A REST API is a web service that follows REST principles, allowing systems to communicate over standard HTTP protocols using structured data (usually JSON or XML).
It maps standard HTTP Verbs to CRUD operations:
- GET
/employees: Retrieves employee data (Read). - POST
/employees: Creates a new employee (Create). - PUT
/employees/101: Updates an employee's data (Update). - DELETE
/employees/101: Deletes the employee (Delete).
The REST Architecture Request Flow
2. The Controller Layer & @RestController
A Controller is the Spring component physically responsible for receiving the incoming HTTP request, routing it to business logic, and sending the response back to the client.
Internal Working of @RestController
@RestController is fundamentally different from the standard @Controller used in Spring MVC.
Internally, it is a meta-annotation composed of two parts: @Controller + @ResponseBody.
@Controllerregisters the class in the IOC container so theDispatcherServletcan route requests to it.@ResponseBodyinstructs Spring to bypass standard view resolution (like trying to find a JSP or Thymeleaf HTML page). Instead, it automatically passes the returned Java object to aHttpMessageConverter(specifically Jackson), serializes it into a JSON string, and writes it directly to the HTTP response body.
3. URL Routing: @RequestMapping
@RequestMapping defines the base URL namespace for an entire controller.
Because of the class-level annotation, the method above is now accessible specifically at GET /employees.
4. The Employee Data Model
Before implementing our HTTP methods, we need a standard Java POJO to represent our data.
5. Fetching Data: @GetMapping & Parameters
Retrieving All Resources
@PathVariable (URL Segments)
Used to extract dynamic values directly from the URL path. Essential for fetching a single specific resource.
Request: GET /employees/101 → The 101 is automatically bound to the id variable.
@RequestParam (Query Parameters)
Used to extract optional data appended after the ? in a URL, usually for searching or filtering.
Request: GET /employees/filter?department=IT&salary=50000
6. Mutating Data: POST, PUT, PATCH, DELETE
@PostMapping & @RequestBody
Used to create brand new resources. The @RequestBody annotation is mandatory. It instructs Spring's MappingJackson2HttpMessageConverter to read the incoming JSON payload and instantly deserialize it into your Java Employee object.
@PutMapping vs @PatchMapping
| Annotation | Purpose | Requirement |
|---|---|---|
@PutMapping |
Complete Update | Client must send the entire object payload. The existing database row is completely overwritten. |
@PatchMapping |
Partial Update | Client sends only the specific fields that changed (e.g., just changing the salary). |
@DeleteMapping
Used specifically to remove a resource. Typically it returns a 204 No Content status or a simple confirmation string.
7. Advanced Endpoints: HTTP Headers
Often APIs require authorization tokens or custom metadata. You extract these using @RequestHeader.
8. Interview Checklist
Top Technical Interview Questions
Q1: What is the exact difference between @Controller and @RestController?
A: @Controller is used in standard Spring MVC applications to return View names (like JSP or Thymeleaf pages) resolved by a ViewResolver. @RestController is a convenience annotation that combines @Controller and @ResponseBody, ensuring the returned Java object is instantly serialized into JSON/XML and written directly to the HTTP response.
Q2: How does @RequestBody work internally?
A: When a request arrives, the DispatcherServlet delegates it to a HandlerAdapter. Detecting @RequestBody, Spring triggers an HttpMessageConverter (specifically the MappingJackson2HttpMessageConverter). This uses Jackson's ObjectMapper to deserialize the incoming JSON payload into the target Java object via reflection.
Q3: What is the difference between @PathVariable and @RequestParam?
A: @PathVariable extracts values embedded directly within the URL path string (e.g., /employees/101), commonly used to identify a specific resource. @RequestParam extracts data from the query string appended after a ? (e.g., /employees?department=IT), commonly used for sorting, filtering, or pagination metadata.
Internal Architecture Classes to Memorize
To demonstrate a senior-level understanding of the REST request pipeline, be prepared to discuss these internal Spring framework components:
DispatcherServlet(The Front Controller)HandlerMapping(SpecificallyRequestMappingHandlerMapping)HandlerAdapterHttpMessageConverterObjectMapper(The core Jackson serialization engine)
Comments
Post a Comment