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