Spring MVC Controllers and @RequestMapping
Spring MVC controllers handle incoming HTTP requests by mapping request characteristics to Java methods. `@RequestMapping` and its HTTP-method-specific variants define which paths and request conditions should invoke those handlers.
A controller is a Spring-managed class that contains request-handling methods. It is commonly annotated with `@Controller`, which marks the class as a web component whose methods can participate in MVC request handling. `@RestController` is a convenient alternative for APIs because it combines controller semantics with response-body behavior.
`@RequestMapping` can be placed on a class, a method, or both. A class-level mapping provides a common path prefix, while a method-level mapping narrows the route to a particular handler. For example, `@RequestMapping("/users")` on a class and `@GetMapping("/{id}")` on a method can produce a GET endpoint at `/users/{id}`.
The annotation can constrain more than a URL path. It can specify HTTP methods, request parameters, headers, and media types. For ordinary REST endpoints, specialized annotations such as `@GetMapping`, `@PostMapping`, `@PutMapping`, `@PatchMapping`, and `@DeleteMapping` usually make the intended HTTP operation clearer than a generic `@RequestMapping`.
Spring's MVC infrastructure examines an incoming request and selects a compatible handler method based on its mapping conditions. If multiple mappings appear applicable, Spring applies its mapping rules to determine the best match; ambiguous mappings should be avoided because they can prevent the application from starting or handling requests predictably.
A handler method can receive information from the request through parameters such as `@PathVariable`, `@RequestParam`, and `@RequestBody`. Its return value can become the HTTP response body, especially when using `@RestController` or `@ResponseBody`.
Key points
- `@Controller` identifies a Spring MVC controller class.
- `@RestController` is commonly used for REST APIs and includes response-body semantics.
- `@RequestMapping` can define class-level and method-level request conditions.
- Specialized mappings such as `@GetMapping` and `@PostMapping` express the HTTP method directly.
- Mappings can constrain paths, HTTP methods, parameters, headers, and media types.
- Request data is commonly bound with annotations such as `@PathVariable`, `@RequestParam`, and `@RequestBody`.
Interview questions
What is the purpose of a Spring MVC controller?
A controller contains methods that handle HTTP requests in a Spring MVC application. Spring discovers the controller as a managed component and uses request mappings to determine which method should handle a matching request. The handler can read request data and produce a response, such as a view or response body. Controllers are therefore the application layer that connects HTTP requests to application logic.
How does `@RequestMapping` work when placed on both a class and a method?
The class-level mapping establishes a common path or other request condition for the controller's handlers. The method-level mapping adds more specific conditions for an individual handler. Their path components are combined, so `@RequestMapping("/users")` followed by `@GetMapping("/{id}")` results in a route such as `/users/42`. This lets related endpoints share a common prefix without repeating it.
What is the difference between `@GetMapping` and `@RequestMapping(method = RequestMethod.GET)`?
Both can express a mapping that accepts GET requests. `@GetMapping` is a specialized annotation designed specifically for GET handlers, while `@RequestMapping` is the more general mapping annotation. The specialized form is typically easier to read because the intended HTTP method is visible immediately. The same pattern exists for annotations such as `@PostMapping` and `@DeleteMapping`.
How do `@PathVariable`, `@RequestParam`, and `@RequestBody` differ?
`@PathVariable` reads a value from a URI path segment, such as the `42` in `/users/42`. `@RequestParam` reads a request parameter, commonly from a query string such as `?active=true`. `@RequestBody` reads the request body and commonly uses HTTP message conversion to turn JSON or another supported representation into a Java object. Choosing the annotation based on where the data is located makes the controller contract clearer.
How does Spring decide which controller method handles an incoming request?
Spring MVC registers handler methods and their request-mapping conditions during application initialization. For an incoming request, the MVC request-mapping infrastructure examines those conditions, including factors such as path, HTTP method, parameters, headers, and media types. It selects the most appropriate matching handler according to the framework's mapping rules. If the application has conflicting mappings that cannot be resolved, the mapping configuration can be considered ambiguous rather than silently choosing an arbitrary method.