You define request mapping in Spring Boot with @RequestMapping or its shortcuts @GetMapping, @PostMapping, @PutMapping, @PatchMapping and @DeleteMapping. Each annotation binds an HTTP method and URL pattern to a controller method, and can narrow the match by parameters, headers and media types.
Put a class-level @RequestMapping on the controller for a shared path prefix, then use the composed annotations on methods. The examples work with Spring Boot 3 and later (Spring Framework 6 and later).
A class-level path is combined with each method-level path.
This exposes GET /api/books, GET /api/books/{id} and POST /api/books. @RestController is @Controller plus @ResponseBody, so return values are written to the response body (as JSON by default when Jackson is on the classpath).
@GetMapping("/x") is shorthand for @RequestMapping(method = RequestMethod.GET, path = "/x"). Prefer the shortcuts because they are shorter and state intent clearly.
If you omit method on @RequestMapping, the handler matches every HTTP method. That is rarely what you want on a method, so restrict it.
Mapping becomes useful when you bind pieces of the request to arguments.
@PathVariable reads a {name} segment from the path.@RequestParam reads a query string or form parameter. It is required unless you set required = false or a defaultValue.@RequestHeader reads a header.Parameter names are matched through the -parameters compiler flag, which Spring Boot's Maven and Gradle plugins enable by default. Otherwise name them explicitly, for example @PathVariable("id").
You can make several handlers share one path and pick between them by the request.
| Attribute | Matches on |
|---|---|
consumes | Request Content-Type |
produces | Request Accept header; sets the response type |
params | Presence or value of a request parameter |
headers | Presence or value of a request header |
When no handler satisfies consumes, Spring replies 415 Unsupported Media Type. When none satisfies produces, it replies 406 Not Acceptable.
Spring MVC uses PathPattern matching by default (since Spring Framework 5.3 and the default in Spring Boot 2.6 and later).
Two things changed in recent versions. Trailing slash matching is off by default since Spring Framework 6, so /api/books/ no longer matches /api/books. And ** is only allowed at the end of a pattern. Recent Spring Framework releases also add first-class API versioning on the mapping annotations (a version attribute); check the reference for your exact version before relying on it, and until then keep the version in the path, such as /api/v1/books.
If two handlers match a request equally well, startup fails with an Ambiguous mapping error. Fix it by making one mapping more specific, for example by adding params, consumes or a distinct path. Unmatched requests return 404, and a known path with a wrong method returns 405 Method Not Allowed.
To inspect what is registered, enable the Actuator mappings endpoint (management.endpoints.web.exposure.include=mappings) and open /actuator/mappings.
@Controller returns view names by default, for use with templates. @RestController adds @ResponseBody so every return value is serialized into the response body.
Yes. @GetMapping is a composed annotation meta-annotated with @RequestMapping(method = RequestMethod.GET). The same applies to the other HTTP-method shortcuts.
Pass an array: @GetMapping({"/a", "/b"}). Both paths invoke the same handler.
The path matches a handler but not for that HTTP method. Add the correct @PostMapping or similar, or fix the method the client sends.
Use a class-level @RequestMapping for the common prefix and @GetMapping, @PostMapping and friends on methods. Bind input with @PathVariable, @RequestParam and @RequestHeader, narrow handlers with consumes, produces and params, and remember that trailing slashes no longer match by default in Spring Framework 6 and later.