@RequestMapping accepts several attributes that together define exactly which requests a handler matches, going beyond just the URL. Understanding these attributes explains how Spring narrows down which method should process a given request.
Key Points: • value or path specifies the URL pattern(s) the method should match. • method restricts matching to specific HTTP verbs, such as RequestMethod.GET or RequestMethod.POST. • params requires certain request parameters to be present (or absent, or equal to a value) for the mapping to match. • headers requires specific HTTP headers to be present for the mapping to match. • consumes restricts matching based on the request's Content-Type, and produces restricts matching based on the response's Content-Type via the Accept header. • name assigns a logical name to the mapping, useful for generating URLs elsewhere in the app.
Example: @RequestMapping(value = "/orders", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") only matches POST requests to /orders that send and expect JSON.
Code Example:
@RequestMapping(
value = "/orders",
method = RequestMethod.POST,
consumes = "application/json",
produces = "application/json"
)
public Order createOrder(@RequestBody Order order) { ... }Interview Tip: A concise interview answer is:
"@RequestMapping supports value or path for the URL, method for the HTTP verb, and consumes/produces to restrict matching by content type. It also has params and headers for finer-grained matching, and name to give the mapping a reusable identifier."