Spring MVC 请求处理与参数绑定笔记(常用 + 不常用,含示例)

一、常用功能及示例

1. 请求映射与方法限定

  • @RequestMapping 多属性配置
    • 作用:灵活限定请求的方法、参数、头、内容类型等。
    • 示例(限定请求方法、参数、请求头):
1
2
3
4
@RequestMapping(value = "/test02", params = {"age=18", "username", "gender!=1"})
public String test02() {
return "test02";
}
  • 示例(限定请求头):
    1
    2
    3
    4
    @RequestMapping(value = "/test03", headers = {"haha"})
    public String test03() {
    return "test03";
    }

示例(限定消费 / 响应内容类型):

1
2
3
4
5
6
7
8
@RequestMapping(value = "/test04", consumes = "application/json")
public String test04() {
return "test04";
}
@RequestMapping(value = "/test05", produces = "text/html;charset=utf-8")
public String test05() {
return "<h1>你好,Spring MVC!</h1>";
}

2. 简单参数绑定(@RequestParam

  • 作用:绑定 URL 普通参数(问号后的 key=value)。
  • 示例
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @RequestMapping("/handle01")
    public String handle01(
    @RequestParam("username") String user1,
    @RequestParam(defaultValue = "123456") String password,
    String cellphone,
    @RequestParam(required = true) String agreement) {
    // 处理参数逻辑
    return "OK";
    }

3. JavaBean 自动绑定

  • 作用:将请求参数自动映射到 JavaBean 的属性中(需属性名与参数名一致)。
  • 示例
1
2
3
4
5
@RequestMapping("/handle03")
public String handle03(Person person) {
System.out.println(person); // 直接输出绑定后的Person对象
return "handle03-OK";
}

4. 请求头绑定(@RequestHeader

  • 作用:获取请求头中的指定字段。
  • 示例
1
2
3
4
5
@RequestMapping("/handle04")
public String handle04(@RequestHeader("host") String host) {
System.out.println(host);
return "ok" + host;
}

5. JSON 请求体绑定(@RequestBody

  • 作用:将请求体的 JSON 数据自动转化为 Java 对象。
  • 示例
1
2
3
4
5
@RequestMapping("/handle07")
public String handle07(@RequestBody Person person) {
System.out.println("person = " + person);
return "ok-"+person;
}

6. 文件上传(MultipartFile

  • 作用:处理单文件或多文件上传。
  • 示例
1
2
3
4
5
6
7
8
9
10
11
12
   @RequestMapping("/handle08")
public String handle08(Person person,
@RequestParam("headerImg") MultipartFile headerImgFile,
@RequestParam("lifeImg") MultipartFile[] lifeImgFiles) throws IOException {
// 处理单文件上传
headerImgFile.transferTo(new File("D:\\Aimg\\" + headerImgFile.getOriginalFilename()));
// 处理多文件上传
for (MultipartFile lifeImgFile : lifeImgFiles) {
lifeImgFile.transferTo(new File("D:\\Aimg\\" + lifeImgFile.getOriginalFilename()));
}
return "ok!!!";
}

7. Servlet API 直接操作(HttpServletRequest/HttpServletResponse

  • 作用:直接使用原生 Servlet API 处理请求响应。
  • 示例
1
2
3
4
5
6
@RequestMapping("/handle10")
public String handle10(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("user");
System.out.println(username);
return username;
}

二、不常用功能

功能 / 注解 作用
HttpEntity<Person> 封装请求头和请求体,可同时获取请求头信息和请求体对象(如 entity.getHeaders()entity.getBody()
矩阵变量(@MatrixVariable 处理 URL 中的矩阵变量(如 /{path};var1=value1;var2=value2 格式)
@CookieValue 专门用于获取 Cookie 中的值
@RequestPart 用于文件上传时的复杂表单(如混合了 JSON 和文件的场景)
Map/Model/ModelMap 服务端渲染时共享数据(结合模板引擎如 Thymeleaf 使用)
@ModelAttribute 前置数据绑定(方法或参数上使用,用于预处理模型数据)
Errors, BindingResult 数据校验结果(结合 @Valid 使用,用于接收校验错误信息)
@SessionAttributes 管理 Session 中的数据
UriComponentsBuilder 封装请求 URL,用于动态构造 URL 地址
@RequestAttribute 获取请求域中的属性(需提前在请求域中设置属性)