Mastering @RequestMapping in Spring Boot: Real‑World Examples & Tips
This article provides a comprehensive guide to using Spring Boot’s @RequestMapping annotation, covering path definitions, HTTP methods, headers, produces/consumes, path variables, parameters, fallback handling, and advanced options, with practical curl commands and code snippets for each scenario.
1. Introduction
@RequestMapping is an annotation in Spring MVC that maps HTTP requests to handler classes or methods. It can be applied at class or method level, allowing developers to define routing rules flexibly.
2. Practical Examples
2.1 Define Path
<code>@RequestMapping(value = "/api/path")
public String path() {
return "define path";
}
</code>Test with:
<code>curl http://localhost:8080/api/path
</code>The value attribute can accept an array to map multiple paths:
<code>@RequestMapping(value = {"/api/path1", "/api/path2"}, method = RequestMethod.GET)
public String path() { ... }
</code>2.2 Set Request Method
<code>@RequestMapping(value = "/api/method", method = RequestMethod.POST)
public String method() {
return "request method";
}
</code>Test with:
<code>curl -i -X POST http://localhost:8080/api/method
</code>Supported methods include GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE.
2.3 Set Request Header
<code>@RequestMapping(value = "/api/header", headers = "x-pack=xxxooo")
public String header() {
return "define Header";
}
</code>Test with:
<code>curl -i -H "x-pack:xxxooo" http://localhost:8080/api/header
</code>Multiple headers can be specified:
<code>@RequestMapping(value = "/api/header",
headers = {"x-pack=xxxooo", "x-key=aabbcc"})
public String header() { ... }
</code> <code>curl -i -H "x-pack:xxxooo" -H "x-key:aabbcc" http://localhost:8080/api/header
</code>2.4 Specify produces
<code>@GetMapping(value = "/api/produces", produces = "application/json")
public String produces() {
return "define produces";
}
</code>This restricts the request to Accept header of application/json, equivalent to setting the header directly.
2.5 Specify consumes
<code>@GetMapping(value = "/api/consumes", consumes = {"text/plain"})
public String consumes() {
return "define consumes";
}
</code>Test with:
<code>curl -H "Content-Type:text/plain" http://localhost:8080/api/consumes
</code>2.6 Use Path Variables
<code>@RequestMapping(value = "/api/var/{id}")
public String pathVar(@PathVariable("id") long id) {
return String.format("id = %d", id);
}
</code>2.7 Set Request Parameters
<code>@RequestMapping(value = "/api/params", params = "id")
public String params(Long id) {
return "params id =" + id;
}
</code>Multiple parameters can be required:
<code>@RequestMapping(value = "/api/params", params = {"id","name"})
public String params(Long id) { ... }
</code>2.8 Fallback Mapping
<code>@RequestMapping(value = "*")
public String fallback() {
return "Fallback for GET Requests";
}
</code>Handles requests when no specific URI matches.
2.9 Ambiguous Mapping Errors
When two controller methods have identical HTTP method, URL, parameters, headers, and media types, Spring throws an ambiguous mapping error.
<code>@PackMapping(value = "/api/params", params = "id")
public String params(Long id) { ... }
@PackMapping(value = "/api/params", params = "id")
public String params1(Long id) { ... }
</code>The following screenshot illustrates such an error:
2.10 Private Handler Methods
<code>@RequestMapping(value = "/api/params", params = "id")
private String params(Long id) {
return "params id =" + id;
}
</code>Controller methods can be private, but this may affect AOP‑based enhancements such as logging.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.