文章目录
- 1. 核心参数注解列表
- 2. 注解详解与对比
- (1)`@RequestParam`
- (2)`@PathVariable`
- (3)`@RequestBody`
- (4)`@ModelAttribute`
- (5)`@RequestHeader`
- (6)`@CookieValue`
- (7)`@SessionAttribute`
- (8)`@RequestPart`
- 3. 注解对比与选择
- 4. 常见问题
- (1)`@RequestParam` vs `@ModelAttribute`
- (2)`@RequestBody` vs `@ModelAttribute`
- (3)何时用 `@PathVariable`?
- 5. 完整示例
在 Java Spring(特别是 Spring MVC 和 Spring Boot)中,控制器方法的参数注解用于绑定请求数据(如 URL 参数、表单数据、JSON 等)到方法参数。以下是常见的参数注解及其区别的详细解析。
1. 核心参数注解列表
注解 | 作用域 | 主要用途 | 支持的请求类型 |
---|
@RequestParam | 方法参数 | 获取 URL 查询参数或表单数据 | GET/POST (表单/查询参数) |
@PathVariable | 方法参数 | 从 URL 路径中提取变量 | 所有 HTTP 方法 |
@RequestBody | 方法参数 | 解析 HTTP 请求体(如 JSON/XML) | POST/PUT/PATCH (Body) |
@ModelAttribute | 方法参数/方法声明 | 绑定请求参数到对象(表单/查询参数) | GET/POST (表单/查询参数) |
@RequestHeader | 方法参数 | 获取 HTTP 请求头字段 | 所有 HTTP 方法 |
@CookieValue | 方法参数 | 获取 Cookie 值 | 所有 HTTP 方法 |
@SessionAttribute | 方法参数 | 从会话(Session)中读取属性 | 所有 HTTP 方法 |
@RequestPart | 极速方法参数 | 处理文件上传(multipart/form-data) | POST (文件上传) |
2. 注解详解与对比
(1)@RequestParam
(2)@PathVariable
(3)@RequestBody
(4)@ModelAttribute
(5)@RequestHeader
- 用途:获取 HTTP 请求头 的值。
- 示例:
@GetMapping("/info")public String getInfo(@RequestHeader("User-Agent") String userAgent) {return "User Agent: " + userAgent;}
- 关键特性:
- 支持默认极速值(
defaultValue
)。 - 可绑定到
Map
获取所有请求头:
public void handle(@RequestHeader Map<String, String> headers)
(6)@CookieValue
(7)@SessionAttribute
(8)@RequestPart
- 用途:处理 文件上传(
multipart/form-data
)。 - 示例:
@PostMapping("/upload")public String uploadFile(@RequestPart("file") MultipartFile file) {String fileName = file.getOriginalFilename();return "Uploaded: " + fileName;}
- 关键特性:
- 适用于
<input type="file">
表单提交。 - 可同时绑定其他非文件参数:
public String upload(@RequestPart("file") MultipartFile file,@RequestParam("desc") String description)
3. 注解对比与选择
场景 | 推荐注解 | 理由 |
---|
获取 URL 查询参数 | @RequestParam | 专为查询参数设计,支持可选参数和默认值。 |
从 URL 路径中提取变量 | @PathVariable | 直接绑定 RESTful 风格的路径变量。 |
解析 JSON/XML 请求体 | @RequestBody | 唯一支持复杂请求体(如 POJO)的注解。 |
绑定表单数据到对象 | @ModelAttribute | 自动填充对象属性,适合传统表单提交。 |
获取请求头 | @RequestHeader | 精确访问特定请求头字段。 |
处理文件上传 | @RequestPart | 专为 multipart/form-data 设计,支持 MultipartFile 。 |
4. 常见问题
(1)@RequestParam
vs @ModelAttribute
- 相同点:均可处理表单数据。
- 区别:
@RequestParam
:逐个绑定简单类型(如 String
, int
)。@ModelAttribute
:自动绑定到对象的字段(如 User
的 name
属性)。
(2)@RequestBody
vs @ModelAttribute
@RequestBody
:- 处理 结构化数据(JSON/XML)。
- 通常用于 API 开发。
@ModelAttribute
:- 处理 表单数据(
application/x-www-form-urlencoded
)。 - 适合传统 Web 应用(如 Thymeleaf + 表单提交)。
(3)何时用 @PathVariable
?
5. 完整示例
@RestController@RequestMapping("/api")public class UserController {@GetMapping("/user/{id}")public String getUser(@PathVariable Long id,@RequestParam(required = false, defaultValue = "false") Boolean details) {return "ID: " + id + ", Details: " + details;}@PostMapping("/user")public User createUser(@RequestBody User user) {return userService.save(user);}@PostMapping("/upload")public String upload(@RequestPart MultipartFile file,@RequestParam String description) {return "File: " + file.getOriginalFilename() + ", Desc: " + description;}@GetMapping("/meta")public String getMeta(@RequestHeader("User-Agent") String userAgent,@CookieValue("JSESSIONID") String sessionId) {return "UA: " + userAgent + ", Session: " + sessionId;}
}