当前位置: 首页 > news >正文

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

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

  • 核心依赖(Maven)
    • 1. 基础 GET 参数绑定
    • 2. 多个参数绑定
    • 3. 对象参数绑定
    • 4. 强制指定请求参数名
    • 5. JSON 请求体绑定
    • 6. 路径参数绑定
    • 7. 文件上传
    • 8. 指定请求方法
    • 9. 多路径映射
    • 10. 使用参数限制


核心依赖(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;
}
http://www.lryc.cn/news/615945.html

相关文章:

  • 第4章 程序段的反复执行4 多重循环练习(题及答案)
  • RAGFlow 拉取 Docker 镜像失败
  • 压力测试等工具源码包编译及使用方法
  • 基于python高校固定资产管理系统
  • 【银行测试】保险项目测试点+测试流程详情(二)
  • scanpy单细胞转录组python教程(一):不同形式数据读取
  • java报错“ NoSuchMethodError:com.test.Service.doRoomList(Ljava/lang/String;)V解决方案
  • Gin 框架错误处理机制详解
  • 线性代数1000题学习笔记
  • 如何将PDF文档进行高效编辑处理!
  • NLP学习开始-02逻辑回归
  • 【Spring IoC 核心实现类详解:DefaultListableBeanFactory】
  • 从策略梯度到 PPO
  • Linux权限管理终极指南(用户身份与文件权限
  • Python中的 __name__
  • 计算机视觉(CV)——pytorch张量基本使用
  • imx6ull-驱动开发篇17——linux原子操作实验
  • docker等基础工具使用
  • 个人笔记Mybatis2
  • 第一章 概述
  • 快速了解DBSCAN算法
  • reinterpret_cast and static cast
  • Docker实战:为项目打造即开即用的宝塔LNMP环境
  • redis集群-docker环境
  • 【从源码角度深度理解 CPython 的垃圾回收机制】:第2课循环引用:标记清除-分代回收
  • 机器学习线性归回实战(单因子和多音字分别建立预测房价模型)
  • 一个基于 Next.js 和 Puppeteer 的 Markdown 转图片服务,支持 Docker 部署和 API 集成
  • Node.js面试题及详细答案120题(01-15) -- 基础概念篇
  • python | numpy小记(十):理解 NumPy 中的 `np.random.multinomial`(进阶)
  • Stlink识别不到-安装驱动