Chapter plan
What you will complete
01Your First REST ControllerRoute HTTP requests to Java methods with @RestController and the mapping annotations, then grow one endpoint into a small resource API.@RestController routes@GetMapping and @PostMappingResource URLs 15 min02The DispatcherServletOne servlet fronts every request: DispatcherServlet consults HandlerMappings, invokes the handler through a HandlerAdapter, and hands the result to converters or view resolvers.Front ControllerHandlerMappingHandlerAdapter 20 min03@RestController vs @Controller@Controller returns view names unless @ResponseBody opts a method into message conversion; @RestController bakes that opt-in in for the whole class.@RestController@RequestMappingMessageConverters 15 min04Request Data BindingEach source of request data has its own annotation: identifiers from the path, options from the query string, metadata from headers, payload from the body.@PathVariable@RequestParam@RequestBody 25 min05Response HandlingResponseEntity controls status, headers, and body together; @ResponseStatus suits fixed codes, and bare returns suit 200-only endpoints.ResponseEntity@ResponseStatusLocation Header 20 min06Content NegotiationThe Accept header picks among the formats your HttpMessageConverters support; negotiation fails loudly when no converter can produce a match.Accept HeaderHttpMessageConverterJackson/JAXB 15 min07DTOs vs Entities: Mapping at the BoundaryGive your public contract its own shape instead of leaking JPA entities through controllers, and map explicitly where the models diverge.Keep request and response DTOs independent of persistence annotationsMap between boundary and domain models in one reviewed placeEvolve the API without turning internal refactors into breaking changes 25 min08Filters vs InterceptorsFilters run in the servlet chain before Spring mapping; interceptors run inside MVC with access to the chosen handler—pick by what each concern needs to see.FilterHandlerInterceptorExecution Order 25 min09Pagination, Versioning & Idempotency KeysKeep list endpoints cheap with pages and slices, evolve the contract without breaking clients, and make retried POSTs safe by design.Bound every collection response with page, size, and sort parametersPrefer additive evolution; version only genuinely breaking changesAccept Idempotency-Key headers to deduplicate client retries 30 min10Test Controllers with MockMvc and WebTestClientProve routing, binding, validation, filters, advice, and JSON contracts without turning every HTTP check into a full application test.Use WebMvcTest for servlet MVC contractsMock application collaborators rather than HTTP infrastructureAssert status, headers, body, and failure payloads together 40 min11Production Implementation: Order API Contract SliceDeliver one reviewable REST slice—DTO contracts, validation, ProblemDetail errors, and MockMvc evidence—exactly as it would ship.Keep controllers as HTTP adaptersProtect public JSON with DTOs and ProblemDetail errorsProve success and failure paths with MockMvc 45 min
Module outcome
Ship endpoints with validated input, consistent error payloads, and tests that prove the HTTP contract.
Mental model
Controllers are HTTP boundaries, not business logic.
Module checkpoint
REST API
Build a versionable task API and trace every request through binding, validation, application logic, and response conversion.
- MockMvc tests pin success, validation, not-found, and conflict contracts
- DTOs prevent persistence or domain internals from leaking into JSON
- A request trace identifies the responsible MVC component at every step
Final module assessment
Ready to prove mastery?
Complete all 50 questions after finishing the module lessons and checkpoint.