Core Spring MVC Annotations for HTTP Request Handling
@RequestMapping
Establishes the routing configuration between incoming HTTP requests and specific controller methods. It functions at both the class and method scopes.
- Class Scope: Sets the primary URL prefix for the entire controller. If omitted, routes resolve from the application root. When defined, it must start with a forward slash (
/). - Method Scope: Appends a secondary path to the class prefix, completing the endpoint URI.
- Configuration Attributes:
value/path: Defines the exact URL pattern to match.method: Restricts the handler to specific HTTP verbs (e.g.,RequestMethod.GET,RequestMethod.POST).params: Filters invocations based on query parameters. Supports exact key-value matching.headers: Narrows mapping eligibility by inspecting specific HTTP headers in the request.
@RequestParam
Maps named query parameters or form submission fields directly to method arguments in the controller.
value: Specifies the exact parameter name expected in the HTTP request.required: Boolean indicator dictating if the parameter is mandatory. Defaults totrue, which triggers a client error if missing. Setting it tofalseallows optional submission.
@Controller
@RequestMapping("/api/accounts")
public class AccountLookupHandler {
@RequestMapping("/verify")
public String processAccountQuery(@RequestParam("user_handle") String loginId,
@RequestParam(value = "is_active", required = false) Boolean isActive) {
System.out.println("Verifying handle: " + loginId + " | Active Status: " + isActive);
return "verificationResult";
}
}
@PathVariable
RESTful design principles advocate for resourec-based URIs where identifiers are embedded directly into the path rather than passed as query strings. @PathVariable extracts these dynamic URI segments and binds them to method parameters, a core capability introduced in Spring 3.0 for robust REST support.
value: Corresponds to the placeholder name enclosed in braces within the mapped URL pattern (e.g.,/orders/{orderId}).required: Enforces the presence of the URI segment.
@Controller
@RequestMapping("/warehouse")
public class InventoryRoutingController {
@RequestMapping("/stock/{zoneCode}/{skuNumber}")
public String locateInventory(@PathVariable("zoneCode") String storageLocation,
@PathVariable(value = "skuNumber", required = true) Long itemReference) {
System.out.println("Scanning Zone: " + storageLocation);
System.out.println("Tracking SKU: " + itemReference);
return "stockSummary";
}
}
@RequestHeader
Retrieves specific metadata from the HTTP request headers and injects it into the controller method.
value: Targets a particular header field (e.g.,Accept-Language,Authorization).required: Determines whether the framework should fail if the specified header is absent.
@Controller
@RequestMapping("/diagnostics")
public class NetworkInspectorController {
@RequestMapping("/analyze-client")
public String captureNetworkHeaders(@RequestHeader(value = "X-Forwarded-For", required = false) String proxyAddress) {
System.out.println("Client Proxy Origin: " + proxyAddress);
return "diagnosticReport";
}
}
@CookieValue
Extracts stored HTTP cookie data and passes it as a method parameter within the controller layer.
value: Identifies the specific cookie key to retrieve.required: Specifies if the cookie must be present in the browser request.
@Controller
@RequestMapping("/analytics")
public class SessionTrackerController {
@RequestMapping("/track-visit")
public String logUserCookie(@CookieValue(name = "VISITOR_ID", required = false) String uniqueToken) {
System.out.println("Logging visitor token: " + uniqueToken);
return "trackingConfirmation";
}
}