SpringMVC之@RequestMapping
一、@RequestMapping注解的作用
浏览器发起请求,被DIspatcherServlet截获请求之后,通过控制器上面的@RequestMapping注解提供的相关信息,来确定处理请求的具体方法.
该注解可以标记在类上:提供初步的请求映射信息,相对于WEB应用的根目录.
该注解可以标记在方法上:提供进一步的细分映射信息,相对于标记在类上的URL.
例如:在类上有@RequestMapping(value = "atClass"),在方法上也有@RequestMapping(value="/atMethod"),如果你想要让你的请求被DispatcherServlet截获之后由testSpringMVC这个方法进行处理,那么你的请求路径必须要满足(http://localhost:8080/SpringMVC/atClass/atMethod, 我这里的项目名是SpringMVC)
1 2 3 4 5 6 7 8 9 | @Controller @RequestMapping (value = "/atClass" ) public class SpringmvcDemo { @RequestMapping (value= "/atMethod" ) public String testSpringMVC(){ System.out.println( "hello world" ); return "test" ; } } |
二、@RequestMapping
2.1、@RequestMapping注解源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @Target ({ElementType.TYPE, ElementType.METHOD}) @Retention (RetentionPolicy.RUNTIME) @Documented @Mapping public @interface RequestMapping { String name() default "" ; // path和value互为别名 @AliasFor ( "path" ) String[] value() default {}; @AliasFor ( "value" ) String[] path() default {}; // RequestMethod是一个枚举类,它的取值如下: // GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE; RequestMethod[] method() default {}; String[] params() default {}; String[] headers() default {}; String[] consumes() default {}; String[] produces() default {}; } |
2.2、@RequestMapping注解之value的取值
下面的例子可以看出,我这里的项目名是SpringMVC,当浏览器发起请求的时候就能通过 /atMethod 去SpringMVC的控制器中寻找相应的映射,当在映射中(@RequestMapping的value值中)找到了value="/atMethod",那么它就能找到对应的处理请求的方法,继而执行相应的逻辑处理,并返回对应的视图.
1 2 | // 浏览器URL http: //localhost:8080/SpringMVC/atMethod |
1 2 3 4 5 6 7 8 9 10 | // SpringMVC的控制器 @Controller public class SpringmvcDemo { @RequestMapping (value= "/atMethod" ) public String testSpringMVC(){ System.out.println( "测试@RequestMapping的value值....." ); // 返回的视图路径是 return "test" ; } } |
1 2 | // 测试结果 测试 @RequestMapping 的value值..... |
SpringMVC不但支持固定的路径,也支持使用占位符来填充请求URL,不过这里要搭配@PathVariable注解来使用,请求会被控制器的testSpringMVC方法进行处理,使用@PathVariable("id") Integer id:的意思是,将路径中可变的id值绑定给Integer类型的id,@PathVariable("name") String name:的意思是将路径中可变的name值绑定至String类型的name.
1 2 | // 浏览器请求URL http: //localhost:8080/SpringMVC/atMethod/9527/xiaomaomao |
1 2 3 4 5 6 7 8 9 | // SpringMVC控制器 @Controller public class SpringmvcDemo { @RequestMapping (value = "/atMethod/{id}/{name}" ) public String testSpringMVC( @PathVariable ( "id" ) Integer id, @PathVariable ( "name" ) String name) { System.out.println(id + " " + name); return "test" ; } } |
1 2 | // 测试结果 9527 xiaomaomao |
2.3、@RequestMapping注解之method的取值
我们可以看出@RequestMapping注解中的method取值是一个RequestMethod类型的注解,它规定了请求的方式该注解的源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // RequestMethod枚举类 public enum RequestMethod { GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE; private RequestMethod() { } } |
案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // RequestMethod.jsp页面 <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>测试 @PathVariable 注解的method的取值!!!</title> </head> <body> <form action= "${pageContext.request.contextPath}/atMethod/001" method= "post" > <input type= "submit" value= "测试POST提交" > </form> <form action= "${pageContext.request.contextPath}/atMethod/002" method= "get" > <input type= "submit" value= "测试GET提交" > </form> </body> </html> |
浏览器发起请求 http://localhost:8080/SpringMVC/RequestMethod.jsp,访问到webapp下面的RequestMethod.jsp页面,页面效果如下:
先点击测试POST提交按钮,接着刷新页面点击测试GET提交按钮
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // SpringMVC控制器 @Controller public class SpringmvcDemo { // 处理POST请求 @RequestMapping (value = "/atMethod/{id}" , method = RequestMethod.POST) public String testSpringMVC01( @PathVariable ( "id" ) Integer id) { System.out.println( "POST请求方式id是" + id); return "test" ; } // 处理GET请求 @RequestMapping (value = "/atMethod/{id}" , method = RequestMethod.GET) public String testSpringMVC02( @PathVariable ( "id" ) Integer id) { System.out.println( "GET请求方式id是" + id); return "test" ; } } |
1 2 3 | // 测试结果 POST请求方式id是 1 GET请求方式id是 2 |
从测试结果中可以看出发起POST方式请求的时候,会被控制器中的method=RequestMethod.POST标注的方法处理该请求,而发起GET方式请求的时候,会被控制器中的method=RequestMethod.GET标注的方法处理请求.
2.4、@RequestMapping注解之param & header的取值
可以使用 params 和 headers 来更加精确的映射请求. params 和 headers 支持简单的表达式
案例:该案例表达的意思是,当请求路径相对于应用路径的值是/atMethod、并且请求方式是GET方式,参数中必须要有name,并且参数id的值是10的情况下,该请求才能匹配到处理请求的方法testSpringMVC01.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // 浏览器请求URL http: //localhost:8080/SpringMVC/atMethod?name&id=10 // SpringMVC控制器 @Controller public class SpringmvcDemo { @RequestMapping (value = "/atMethod" , method = RequestMethod.GET, params = { "name" , "id=10" }) public String testSpringMVC01() { System.out.println( "只有请求参数中存在name,并且id=10的时候才能进行匹配!!!" ); return "test" ; } } // 测试结果 只有请求参数中存在name,并且id= 10 的时候才能进行匹配!!! |
案例:
1 2 3 4 | // 请求路径相对应用路径为/atMethod,请求方式为POST,参数中userName=xiaomaomao&password=1001,并且请求头中的参数满足如下情况的时候才能正确的映射到控制器中相应的方法中. @RequestMapping (value = "/atMethod" , method = RequestMethod.POST, params = { "userName=xiaomaomao" , "password=1001" }, headers={ "Accept-Language=zh-CN,zh;q=0.9" , "User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36" }) |
三、@RequestMapping支持Ant路径
Ant风格资源地址支持三种通配符
1、?:匹配路径中的一个字符
2、*:匹配路径中的任意一个字符,包括0个.
3、**:匹配任意层路径
支持的访问URL | @RequestMapping的取值 |
/atMethod/auser /atMethod/buser 不支持 /atMethod/user /atMethod/abuser |
@RequestMapping(value = "/atMethod/?user", method = RequestMethod.GET)
|
/atMethod/user /atMethod/auser /atMethod/xuser /atMethod/abcuser |
@RequestMapping(value = "/atMethod/*user", method = RequestMethod.GET)
|
/atMethod/user /atMethod/aa/user /atMethod/aa/bb/user |
@RequestMapping(value = "/atMethod/**/user", method = RequestMethod.GET)
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?