Web MVC — *From Request to Response*
Web MVC — *From Request to Response*
🎯 After reading this lesson
After reading this lesson, you will be able to confidently do the following 3 things.
- ▸✅ Implement 4 types of REST API (GET/POST/PUT/DELETE) using @RestController + @RequestMapping
- ▸✅ Understand and use the differences between @RequestBody / @PathVariable / @RequestParam
- ▸✅ Set up global exception handling patterns with @RestControllerAdvice
Keep these learning goals as a checklist — once you can answer all of them, close the lesson.
*How* Requests Are Processed — The DispatcherServlet Flow
Key Takeaway
When a browser sends a GET /users/42 request, Spring routes it through DispatcherServlet — a traffic controller — and delivers it to the right controller.
6-Step Flow
1. HTTP request arrives — received by the embedded Tomcat
2. DispatcherServlet — Spring's front door. All requests pass through here
3. HandlerMapping — "Which method on which controller handles this URL?"
4. Controller executes — business logic + Service call
5. Response object — DTO returned → Jackson serializes it to JSON
6. HTTP response — sent back to the browser
Each step is handled automatically by Spring. As a developer, you only need to write a @GetMapping("/users/{id}") method inside a @RestController class.
Filter vs Interceptor vs AOP — Hooking into the Middle
When you want to inject common logic (authentication, logging, compression) into the request processing pipeline, you have 3 options.
Filter — Servlet Standard
Runs before DispatcherServlet. Handles the entire HTTP response.
Use for: common to all requests — logging, compression, CORS, auth token extraction
Interceptor — Spring MVC Layer
Runs after DispatcherServlet, hooks in before/after the controller is called.
Use for: targeting specific controller patterns. URL-based authorization and logging.
AOP — Method Level
Before/after specific method calls. The most granular option.
@Transactional, @Cacheable, and similar annotations are all AOP-based.
When to use what?
- ▸Common to all requests → Filter
- ▸URL pattern-based → Interceptor
- ▸Specific methods → AOP
Summary
DispatcherServlet receives all requests and routes them to the appropriate controller. To inject common logic in between, choose from Filter, Interceptor, or AOP. The more granular the need, the further back you go.
@RestController + @GetMapping — *Building a REST API*
@Controller vs @RestController
The older @Controller in Spring returns a View (HTML). It worked with template engines like JSP and Thymeleaf for server-side rendering (SSR).
@RestController returns data (JSON). The standard for REST APIs communicating with SPAs and mobile apps. Essentially a combination of @Controller + @ResponseBody.
The returned UserDto object is automatically converted to JSON by Jackson and sent to the browser. Clean and concise.
HTTP Method Mapping
All are shorthand for @RequestMapping(method = RequestMethod.GET). Always use these shorthand forms for explicit semantic meaning.
4 Ways to Receive Parameters
1. @PathVariable — variable in the URL path:
2. @RequestParam — query string:
3. @RequestBody — JSON body (POST/PUT):
4. @RequestHeader / @CookieValue — HTTP headers and cookies:
Response — Specifying HTTP Status Codes
Returning a value alone defaults to 200 OK. When you need a different status:
Exception Handling — @RestControllerAdvice
Instead of scattering try-catch blocks across every controller, configure global exception handling:
Now, throwing EntityNotFoundException from any controller → automatically produces a 404 response.
Input Validation — @Valid
Validation failure → MethodArgumentNotValidException is thrown automatically → the GlobalExceptionHandler above returns a clean 400 response.
Summary
- ▸
@RestController+@GetMappingfor instant REST API setup - ▸
@PathVariable,@RequestParam,@RequestBodyfor input - ▸
@ResponseStatus,ResponseEntityfor status codes - ▸
@RestControllerAdvicefor global exception handling - ▸
@Valid+ Bean Validation for input validation
🤖 Try asking AI like this
Knowing the concepts from this lesson lets you give AI specific, precise instructions. Instead of a vague 'fix this,' you make vocabulary-driven requests — and that's where token savings start.
- ▸'Add @ControllerAdvice-based global exception handling to this controller'
- ▸'Create a User CRUD API with 4 endpoints (GET/POST/PUT/DELETE)'
- ▸'Wrap this response in a ResponseEntity and return exactly 201 / 204 status codes'
Why This Reduces Tokens
Without the concepts, you have to ask 'what does that mean?' after every AI response. Those follow-up questions eat your tokens. Master the concepts once, and the conversation ends in a single exchange.