Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Spring’s @RequestMapping connects incoming web requests to controller classes and methods. Put a mapping on a controller class to define a shared route or condition, then add a method-level mapping to identify the operation. For endpoints with a known HTTP method, prefer @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, or @PatchMapping instead of an unconstrained method-level @RequestMapping.

The details below follow the Spring Framework 7.0.9 MVC reference and Javadoc. WebFlux also supports the annotation, but its reactive handler infrastructure and your project’s Spring version should be checked separately.

What @RequestMapping does

Spring’s official description is: “You can use the @RequestMapping annotation to map requests to controllers methods.” See the Spring MVC mapping reference.

The annotation can target a controller type or a handler method. A type-level mapping supplies shared conditions; the method-level mapping adds the endpoint-specific conditions. The annotation is retained at runtime and is supported by both Spring MVC and Spring WebFlux, as documented in the RequestMapping Javadoc.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@Controller
@RequestMapping("/persons")
class PersonController {

    @GetMapping("/{id}")
    Person get(@PathVariable long id) {
        // ...
    }

    @PostMapping
    Person create(@RequestBody Person person) {
        // ...
    }
}

Here, /persons is the shared class route. The first method handles GET /persons/{id}; the second handles POST /persons.

Class-level and method-level mappings

Use the class level for a shared route

A class-level declaration such as @RequestMapping("/persons") keeps a controller’s endpoints under one URL prefix. It can also define shared HTTP-method, parameter, header, or media-type conditions.

Use the method level for the operation

Method-level mappings identify the concrete path and operation. A method path is combined with the class path, while method-level conditions narrow the candidate handler. A method can use either @RequestMapping with attributes or a composed HTTP-method annotation.

Do not stack mapping annotations on one element

Do not place multiple request-mapping annotations on the same class or method in an attempt to merge their conditions, for example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@GetMapping("/items")
@RequestMapping(produces = "application/json") // not a composition mechanism

Spring logs a warning when it detects multiple mappings on one element and uses only the first detected mapping. This also applies when one of the annotations is a composed variant such as @GetMapping.

Choose an HTTP-method-specific mapping

A bare @RequestMapping has no HTTP-method restriction, so it can match all HTTP methods by default. That is rarely the intended behavior for an individual controller operation. Use a composed annotation when the endpoint has a known method:

Annotation HTTP method Typical use
@GetMapping GET Read a resource
@PostMapping POST Create or submit data
@PutMapping PUT Replace a resource
@DeleteMapping DELETE Delete a resource
@PatchMapping PATCH Partially modify a resource

These are composed forms of request mapping for common HTTP methods. A class-level @RequestMapping remains useful for the shared path.

Conditions Spring can match

Path and URI patterns

Spring MVC’s current reference uses parsed PathPattern patterns. Supported forms include:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Literal paths such as /orders.
  • ? for one character.
  • * for zero or more characters within one path segment.
  • ** for zero or more path segments in permitted positions.
  • Named URI variables such as /{id}.
  • Regex-constrained variables such as /{name:[a-z-]+}.

The reference warns that ** cannot appear in the middle of a path, and a pattern may contain only one ** or {*path} instance. The older AntPathMatcher approach is described as deprecated in the current documentation.

HTTP method

Declare a method with method = RequestMethod.GET or use the corresponding composed annotation. Explicit declarations make the endpoint’s contract clear and prevent an unconstrained handler from accepting unintended methods.

Request parameters and headers

Parameter and header conditions can require that a value be present, require it to be absent, or match a particular value. For example, a mapping can distinguish requests containing a specific query parameter or header without creating separate URL paths.

Request media type with consumes

consumes matches the request’s Content-Type. It is appropriate when a handler accepts a particular representation, such as JSON:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@PostMapping(path = "/orders", consumes = "application/json")
Order create(@RequestBody Order order) { ... }

Media-type expressions support negation, so a mapping can exclude a type as well as require one.

Response media type with produces

produces describes the representations a handler can return and is matched against the request’s Accept header:

@GetMapping(path = "/orders/{id}", produces = "application/json")
Order find(@PathVariable long id) { ... }

consumes concerns the incoming representation; produces concerns the outgoing representation.

API version conditions

Spring Framework 7.0.9’s MVC reference documents a version mapping attribute when API versioning has been enabled in MVC configuration. It describes fixed versions, baseline expressions such as 1.2+, and unversioned handlers; the most specific applicable version takes precedence. The requested version must be configured as supported. This is Spring’s configured mechanism, not a universal HTTP standard, and the annotation syntax alone does not enable versioning.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Important precedence and protocol behavior

Method-level media types replace class-level values

If a controller declares class-level consumes or produces, a method-level declaration replaces that condition; it does not extend or merge the class-level list. Review both levels whenever a handler unexpectedly rejects a content type or an Accept header.

HEAD and OPTIONS

Spring MVC handles HEAD through a matching GET mapping. It also supplies default OPTIONS handling. The generated Allow header is based on methods mapped to matching URL patterns. When no HTTP method is declared, the documented allowed set is GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS. Explicitly declare the methods an endpoint supports rather than relying on that broad default.

Interface-based controllers and proxies

When controller interfaces are used, including cases involving AOP proxies, the Javadoc advises placing all mapping annotations consistently on the interface rather than splitting them between the interface and implementation class. Keeping the mapping metadata in one location avoids discovery differences caused by proxying and annotation lookup.

MVC versus WebFlux

Spring MVC is the Servlet API-based web framework; Spring WebFlux is the reactive stack. Both support @RequestMapping, but they use separate handler-mapping infrastructure. Confirm the behavior and available options in the reference for the stack and Spring Framework version used by your application. The MVC framework overview is at Spring Web MVC.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A practical mapping checklist

  • Put the shared URL prefix or shared conditions on the controller class.
  • Give each handler one mapping annotation only.
  • Use an HTTP-method-specific composed annotation for ordinary endpoint methods.
  • Check that path patterns obey PathPattern rules, especially for **.
  • Use consumes for the request Content-Type and produces for acceptable response media types.
  • Remember that method-level media-type declarations replace class-level declarations.
  • Enable and configure API versioning before using the version condition.
  • Keep mapping annotations consistently on controller interfaces when interfaces are used for proxying.
  • Verify behavior against the application’s actual Spring MVC or WebFlux version.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.