@RequestMapping tells Spring Boot which controller method handles which HTTP request. It matches the URL path and can further match the HTTP method, query parameters, headers and media types. Spring Boot auto-configures Spring MVC, so you simply annotate your controllers and the mappings are registered at startup.
For single methods, @GetMapping, @PostMapping and similar shortcuts are the preferred style. @RequestMapping is most useful on the class, for a shared base path, and when you need conditions the shortcuts do not express.
At startup, RequestMappingHandlerMapping scans beans annotated with @Controller or @RestController, reads their @RequestMapping metadata and stores a registry. DispatcherServlet consults that registry for each request, picks the best match, resolves method arguments and invokes the method. You can list the registered mappings through the Actuator mappings endpoint.
Then open /actuator/mappings to see every route, which is handy when a mapping does not behave as expected. Secure this endpoint in production.
GET http://localhost:8080/api/hello returns Hello, Spring Boot!. The class path /api and the method path /hello are joined. @RestController is @Controller plus @ResponseBody, so the string is written directly to the response body instead of being treated as a view name.
Without a method, a mapping accepts every HTTP method. Restrict it:
The shorter equivalents are @GetMapping("/items"), @PutMapping, @PatchMapping, @PostMapping and @DeleteMapping. When the path matches but the method does not, Spring answers 405 Method Not Allowed and lists the allowed methods in the Allow header.
Run curl -i -X POST localhost:8080/api/tasks -H "Content-Type: application/json" -d '{"title":"Write docs"}' and you get 201 Created with a Location header.
consumes is matched against the request Content-Type and produces against the Accept header, which allows several handlers on one URL.
Ambiguous mapping error at startup./api/tasks/ and /api/tasks are different.@Controller method returns a String, it is a view name. Add @ResponseBody or use @RestController for JSON or text.scanBasePackages.@PathVariable("name") unless you compile with the -parameters flag, which Spring Boot's build plugins enable by default.Use @GetMapping and its siblings for methods, and @RequestMapping for class-level prefixes or unusual combinations.
Yes. Spring supports mapping annotations on interface methods, which some teams use for shared contracts. Parameter annotations are inherited from Spring Framework 5.1 onward.
Enable the Actuator mappings endpoint or enable debug logging for org.springframework.web.
Yes, @RequestMapping({"/a", "/b"}) accepts an array of paths.
@RequestMapping is Spring MVC's routing annotation, and Spring Boot wires it up automatically. Put a shared prefix on the class, use the @GetMapping family for methods, restrict by HTTP method and media type, and inspect the Actuator mappings endpoint when debugging.