SpringMVC路径映射
# SpringMVC路径映射
@RequestMapping 是 Spring MVC 中最核心的注解之一,它用于将 HTTP 请求映射到控制器(Controller)的处理方法上。简单来说,它告诉 Spring:“当客户端发送一个特定的请求时,应该由哪个方法来处理它。”
# 一、基本概念与核心作用
- 作用:建立 URL 路径与 Java 方法的映射关系,让 Spring MVC 框架能根据请求 URL 找到对应的处理方法。
- 位置:可用于类级别或方法级别。
- 类级别:规定该控制器所有方法的统一访问前缀。
- 方法级别:具体指定某个方法的访问路径。
# 二、常用属性
@RequestMapping 有多个属性,可组合使用以精确匹配请求。
| 属性名 | 类型 | 作用 | 示例 |
|---|---|---|---|
value / path | String[] | 指定请求的 URL 路径(最核心属性)。 | @RequestMapping("/users") 或 @RequestMapping(path = "/users") |
method | RequestMethod[] | 指定请求的 HTTP 方法(如 GET, POST, PUT, DELETE)。 | @RequestMapping(value = "/users", method = RequestMethod.GET) |
params | String[] | 指定请求必须包含的参数。 | @RequestMapping(value = "/users", params = "id") 表示请求必须带 id 参数。 |
headers | String[] | 指定请求必须包含的请求头。 | @RequestMapping(value = "/users", headers = "Content-Type=application/json") |
consumes | String[] | 指定请求体的媒体类型(Content-Type)。 | @RequestMapping(value = "/users", consumes = "application/json") |
produces | String[] | 指定响应体的媒体类型。 | @RequestMapping(value = "/users", produces = "application/json") |
注意:如果只使用 value 属性,可以省略属性名,直接写路径,如 @RequestMapping("/users")。
# 三、使用示例
# 1. 基础用法(类级别 + 方法级别)
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/app") // 类级别注解,所有方法的URL都以 /app 开头
public class MyController {
// 方法级别注解,完整URL是 /app/hello
@RequestMapping("/hello")
@ResponseBody // 直接将返回值作为响应体返回
public String sayHello() {
return "Hello, Spring MVC!";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
当访问 http://localhost:8080/app/hello 时,会调用 sayHello 方法并返回 "Hello, Spring MVC!"。
# 2. 限制请求方法
@Controller
@RequestMapping("/users")
public class UserController {
// 只接受 GET 请求
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public String getUserList() {
return "List of all users";
}
// 只接受 POST 请求
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
public String createUser() {
return "User created successfully";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 访问
GET /users/list→ 正确。 - 访问
POST /users/create→ 正确。 - 访问
POST /users/list→ 报错(405 Method Not Allowed)。
# 3. 处理带参数的请求
@Controller
@RequestMapping("/products")
public class ProductController {
// 要求请求必须包含 "category" 和 "price" 参数
@RequestMapping(value = "/search", params = {"category", "price"})
@ResponseBody
public String searchProducts(String category, int price) {
return "Searching for " + category + " products under $" + price;
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
当访问 http://localhost:8080/products/search?category=books&price=50 时,会调用 searchProducts 方法,并传入参数 category=books 和 price=50。
# 四、组合注解(Spring 4.3+)
为了简化常用场景的写法,Spring 提供了一系列组合注解,它们本质上是 @RequestMapping 的特例:
| 组合注解 | 等价于 | 用途 |
|---|---|---|
@GetMapping | @RequestMapping(method = RequestMethod.GET) | 处理 GET 请求 |
@PostMapping | @RequestMapping(method = RequestMethod.POST) | 处理 POST 请求 |
@PutMapping | @RequestMapping(method = RequestMethod.PUT) | 处理 PUT 请求 |
@DeleteMapping | @RequestMapping(method = RequestMethod.DELETE) | 处理 DELETE 请求 |
@PatchMapping | @RequestMapping(method = RequestMethod.PATCH) | 处理 PATCH 请求 |
示例:
@Controller
@RequestMapping("/api/users")
public class UserApiController {
@GetMapping
@ResponseBody
public String getAllUsers() {
return "All users";
}
@GetMapping("/{id}") // 路径变量,后面会讲
@ResponseBody
public String getUserById(@PathVariable int id) {
return "User with ID: " + id;
}
@PostMapping
@ResponseBody
public String addUser() {
return "User added";
}
@PutMapping("/{id}")
@ResponseBody
public String updateUser(@PathVariable int id) {
return "User " + id + " updated";
}
@DeleteMapping("/{id}")
@ResponseBody
public String deleteUser(@PathVariable int id) {
return "User " + id + " deleted";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 五、高级用法:路径变量(@PathVariable)
@RequestMapping 支持在 URL 中嵌入变量,通过 @PathVariable 注解将其绑定到方法参数上。
@Controller
@RequestMapping("/users")
public class UserController {
// URL 中的 {id} 是一个变量
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public String getUserById(@PathVariable int id) {
return "User ID: " + id;
}
// 支持多个变量和正则表达式
@RequestMapping("/{username:[a-z]+}/profile")
@ResponseBody
public String getUserProfile(@PathVariable String username) {
return "Profile of user: " + username;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 访问
GET /users/101→ 调用getUserById(101)→ 返回 "User ID: 101"。 - 访问
GET /users/john/profile→ 调用getUserProfile("john")→ 返回 "Profile of user: john"。 - 访问
GET /users/123/profile→ 报错(404 Not Found),因为username必须是小写字母。
Last Updated: 2025/12/02, 11:22:00