Spring Boot 注解详解:@RequestMapping 的多种用法

发布于:2025-08-11 ⋅ 阅读:(20) ⋅ 点赞:(0)


核心依赖(Maven)

在 pom.xml 中添加 Spring Boot Web 依赖

  <!-- Spring Boot Web 模块 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

1. 基础 GET 参数绑定

@RequestMapping("/t1")
public String test1(String name) {
    return "hello " + name + " " + System.currentTimeMillis();
}

说明:

  • @RequestMapping("/t1"):映射路径 /t/t1(因为类上还有一个 /t 前缀)。
  • 方法参数 String name:Spring Boot 会自动根据请求参数名绑定值。
  • 适用于简单 GET 请求参数。

访问示例:

GET http://localhost:8080/t/t1?name=Tom
返回:hello Tom 1691380823456

2. 多个参数绑定

@RequestMapping("/t2")
public String test2(String name1, String name2) {
    return "hello " + name1 + " " + name2;
}

说明:

  • 同样使用 @RequestMapping,接收多个参数。
  • Spring 会自动匹配 URL 里的 name1name2

访问示例:

GET http://localhost:8080/t/t2?name1=Tom&name2=Jerry
返回:hello Tom Jerry

3. 对象参数绑定

@RequestMapping("/t3")
public String test3(student student) {
    return "hello " + student.toString();
}

说明:

  • 请求参数会自动映射到对象的同名属性上。
  • student 类必须有 nameage 等属性,并有 set/get 方法。

访问示例:

GET http://localhost:8080/t/t3?name=Tom&age=18
返回:hello student{name='Tom', age=18}

4. 强制指定请求参数名

@RequestMapping("/t4")
public String test4(@RequestParam("sname") String name) {
    return "hello " + name;
}

说明:

  • @RequestParam("sname"):绑定请求参数 sname 到方法参数 name
  • 如果请求里没有 sname 参数,会报错(除非设置 required=false)。

访问示例:

GET http://localhost:8080/t/t4?sname=Tom
返回:hello Tom

5. JSON 请求体绑定

@RequestMapping("/t5")
public String test5(@RequestBody student student) {
    return "hello " + student.toString();
}

说明:

  • @RequestBody:将请求体中的 JSON 转成 Java 对象。
  • 适合 POST 请求 JSON 数据。

访问示例(POST JSON)

POST http://localhost:8080/t/t5
Content-Type: application/json
Body:
{
    "name": "Tom",
    "age": 18
}
返回:hello student{name='Tom', age=18}

6. 路径参数绑定

@RequestMapping("/t6/{id}/{name}")
public String test6(@PathVariable Integer id, @PathVariable String name) {
    return "hello " + id + " " + name;
}

说明:

  • @PathVariable:从 URL 路径中取值。
  • 路径参数名要和占位符一致。

访问示例:

GET http://localhost:8080/t/t6/123/Tom
返回:hello 123 Tom

7. 文件上传

@RequestMapping("/t7")
public String test7(@RequestParam("file") MultipartFile file) throws IOException {
    String filename = file.getOriginalFilename();
    file.transferTo(new File("D:/jtest/demo1" + filename));
    return "hello " + filename;
}

说明:

  • MultipartFile 用于接收上传文件。
  • file.transferTo() 保存文件到本地路径。

访问示例(POST form-data):

  • Key:file(类型 file)
  • Value:选择一个本地文件
  • 返回:hello test.jpg

8. 指定请求方法

使用 method 属性可以限定 HTTP 请求类型。

@RequestMapping(value = "/getUser", method = RequestMethod.GET)
public User getUser() {
    return new User("Tom", 20);
}

常用方法类型:

  • RequestMethod.GET
  • RequestMethod.POST
  • RequestMethod.PUT
  • RequestMethod.DELETE

9. 多路径映射

@RequestMapping 支持多个 URL 映射。

@RequestMapping(value = {"/home", "/index"})
public String home() {
    return "首页";
}

上例中 /home/index 都会映射到 home() 方法。


10. 使用参数限制

可以通过 params 属性限定请求参数(访问时必须带name参数)。

@RequestMapping(value = "/search", params = "name")
public String search(String name) {
    return "搜索结果:" + name;
}

网站公告

今日签到

点亮在社区的每一天
去签到