| @RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系 |
| SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求 |
| @RequestMapping标识一个类:设置映射请求的请求路径的初始信息 |
| @RequestMapping标识一个方法:设置映射请求请求路径的具体信息 |
| @RequestMapping注解的value属性通过请求的请求地址匹配请求映射 |
| @RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址 |
| 所对应的请求 |
| @RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射 |
| |
| # case |
| @RequestMapping( |
| value = {"/testRequestMapping", "/test"} |
| ) |
| @RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射 |
| @RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配 |
| 多种请求方式的请求 |
| 若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错 |
| 405:Request method 'POST' not supported |
| |
| # case |
| @RequestMapping( |
| value = {"/testRequestMapping", "/test"}, |
| method = {RequestMethod.GET, RequestMethod.POST} |
| ) |
| 处理get请求的映射 |
| 处理post请求的映射 |
| 处理put请求的映射 |
| 处理delete请求的映射 |
| @RequestMapping注解的params属性通过请求的请求参数匹配请求映射 |
| @RequestMapping注解的params属性是一个字符串类型的数组,可以通过四种表达式设置请求参数 |
| 和请求映射的匹配关系 |
| "param":要求请求映射所匹配的请求必须携带param请求参数 |
| "!param":要求请求映射所匹配的请求必须不能携带param请求参数 |
| "param=value":要求请求映射所匹配的请求必须携带param请求参数且param=value |
| "param!=value":要求请求映射所匹配的请求必须携带param请求参数但是param!=value |
| |
| # case: 参数中必须包括username,且password不能为123456 |
| @RequestMapping( |
| value = { |
| "/testRequestMapping", "/test"} |
| ,method = {RequestMethod.GET, RequestMethod.POST} |
| ,params = {"username","password!=123456"} |
| ) |
| @RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射 |
| @RequestMapping注解的headers属性是一个字符串类型的数组,可以通过四种表达式设置请求头信 |
| 息和请求映射的匹配关系 |
| "header":要求请求映射所匹配的请求必须携带header请求头信息 |
| "!header":要求请求映射所匹配的请求必须不能携带header请求头信息 |
| "header=value":要求请求映射所匹配的请求必须携带header请求头信息且header=value |
| "header!=value":要求请求映射所匹配的请求必须携带header请求头信息且header!=value |
| 若当前请求满足@RequestMapping注解的value和method属性,但是不满足headers属性,此时页面 |
| 显示404错误,即资源未找到 |
- 查看请求头中的属性

| |
| @RequestMapping( |
| value = { |
| "/testRequestMapping", "/test"} |
| ,method = {RequestMethod.GET, RequestMethod.POST} |
| ,params = {"username","password!=123456"} |
| ,headers = {"Host=localhost:8081"} |
| ) |
| |
| @RequestMapping("/a?a/test") |
| |
| http://localhost:8080/ada/test |
| |
| http://localhost:8080/aa/test |
| http://localhost:8080/adda/test |
| http://localhost:8080/a?a/test |
| http://localhost:8080/a/a/test |
| |
| *:表示任意的0个或多个字符 |
| @RequestMapping("/a*a/test") |
| |
| http://localhost:8080/aa/test |
| http://localhost:8080/adda/test |
| |
| http://localhost:8080/a?a/test |
| http://localhost:8080/a/a/test |
| |
| **:表示任意的一层或多层目录 |
| @RequestMapping("/a**a/test") |
| |
| http://localhost:8080/aa/test |
| http://localhost:8080/adda/test |
| |
| http://localhost:8080/a?a/test |
| http://localhost:8080/a/a/test |
| |
| @RequestMapping("/**/test") |
| |
| http://localhost:8080/a/a/test |
| http://localhost:8080/a/a/a/test |
| @RequestMapping("/testRest/{id}/{username}") |
| public String testRest(@PathVariable("id") String id, @PathVariable("username") String username){ |
| System.out.println("id:"+id+", username:"+username); |
| return "success"; |
| } |
| |
| # http: |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?