web layer
REST Controllers & Routing
Expose HTTP endpoints via @RestController, map URLs to methods with @GetMapping/@PostMapping, and bind path/query/body parameters automatically.
A REST controller is the entry point that turns an incoming HTTP request into a call on your Java code and turns the return value back into an HTTP response. Spring MVC handles the plumbing — routing, parameter binding, and serialisation — so a controller method reads as a clean mapping from a URL and verb to a piece of behaviour, with no manual parsing of the request or hand-assembly of the response.
A restaurant menu — each URL is a dish, each parameter is a side. The waiter (Spring) translates between customer requests and kitchen calls.
Key Concepts
1
You annotate a class with @RestController (which combines @Controller and @ResponseBody, so return values are written directly to the response body rather than resolved as view names). Methods are mapped with @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping, optionally rooted under a class-level @RequestMapping path. Spring binds the parts of the request automatically: @PathVariable pulls a value out of the URL template, @RequestParam reads a query parameter, and @RequestBody deserialises the JSON payload into a Java object via Jackson. Returning a plain object serialises it to JSON with a 200 status, while returning a ResponseEntity lets you control the status code, headers, and body explicitly — useful for 201 Created with a Location header, or 404 when something is missing.
@RestController@Controller@ResponseBody@GetMapping@PostMapping
2
The design points worth raising are keeping controllers thin — they should validate input, delegate to a service, and shape the response, not contain business logic — and mapping HTTP semantics correctly: the right verbs, the right status codes, and DTOs rather than leaking JPA entities straight to the wire. Common gotchas include forgetting that @RequestBody needs a parseable content type, and confusing @PathVariable with @RequestParam when a value can come from either the path or the query string.
@RequestBody@PathVariable@RequestParam
3
What the interviewer is probing.
What the interviewer is probing.
4
1. "What does @RestController add over @Controller?" *Probing:* the detail. *Stalls:* "Nothing."
*Moves up:* it combines @Controller with @ResponseBody, so return values are serialised to the body
rather than resolved as view names.
1. "What does @RestController add over @Controller?"
5
2. "How do you return a specific status code?" *Probing:* the practical skill. *Stalls:* "Throw
an exception." *Moves up:* ResponseEntity for per-case control, or @ResponseStatus for a fixed one.
2. "How do you return a specific status code?"
6
3. "What is the difference between @RequestParam and @PathVariable?" *Probing:* the basics.
*Stalls:* "Nothing." *Moves up:* query string versus a templated segment of the path.
3. "What is the difference between @RequestParam and @PathVariable?"
7
4. "How do you handle content negotiation?" *Probing:* the depth. *Stalls:* "You cannot." *Moves
up:* produces and consumes on the mapping, with the Accept header selecting the representation.
4. "How do you handle content negotiation?"