Spring MVC
Spring MVC is the web layer that maps HTTP to Java methods. A DispatcherServlet picks a handler, binds parameters, runs interceptors, and writes the body. @RestController is the JSON-facing form.
Detailed Explanation
A request arrives at DispatcherServlet. Handler mappings find a @GetMapping("/users/{id}") method. HandlerAdapter invokes it. Argument resolvers bind @PathVariable, @RequestParam, @RequestBody. Return value handlers write JSON (@ResponseBody on @RestController) or a view name. Exception resolvers turn exceptions into status codes (@ControllerAdvice).
MVC is the servlet programming model. Spring WebFlux is the reactive cousin. Most services still use MVC. Controllers should be thin: validate, call a service, map to a DTO. Business rules in a controller are hard to test without MockMvc.
@Valid plus BindingResult or a 400 from a MethodArgumentNotValidException advice is the validation path. Content types matter: produces and consumes avoid ambiguous mappings. Two methods on the same path with the same verb fail startup.
You reach for MVC when you expose HTTP in Spring. You reach for RestClient/WebClient on the client side. You reach for a raw servlet for a single special endpoint you do not want in the MVC graph.
The cost of a fat controller is untested logic. The cost of returning entities from JPA is lazy-load exceptions and leaked columns. Use DTOs. The cost of an unhandled exception is a 500 with a stack in the body in dev; do not do that in prod.
MockMvc tests the MVC slice without a port. WebMvcTest loads controllers, not the full context.
Keep URL design in the controller and policy in the service. Status codes belong to the HTTP layer: 404 when the service returns empty, 409 when it reports a conflict. Do not throw a generic RuntimeException and hope an advice maps it. Name exception types you mean to expose. MVC will then stay a thin adapter between HTTP and Java, which is the whole point of the pattern.
Code Examples
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
class UsersController {
@GetMapping("/users/{id}")
UserDto one(@PathVariable String id) {
return new UserDto(id, "Ana");
}
record UserDto(String id, String name) {}
}import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
class Errors {
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
String bad(IllegalArgumentException e) {
return e.getMessage();
}
}Common Gotchas
Ambiguous mappings fail at startup
same path, same verb, same params.
`@RestController` = `@Controller` + `@ResponseBody`
`@RestController` = `@Controller` + `@ResponseBody`
Business logic in the controller skips the service tests you should have
Business logic in the controller skips the service tests you should have