@RequestMapping is one of the most widely used Spring MVC annotation. org.springframework.web.bind.annotation.RequestMapping
annotation is used to map web requests onto specific handler classes and/or handler methods.
@RequestMapping
can be applied to the controller class as well as methods. Today we will look into various usage of this annotation with example and other annotations @PathVariable
and @RequestParam
.
春天请求
- 联合国 请求 与类相映射**: 我们可以用它与类定义来创建基URI. 例如:
1@ Captain
2@ RequestMapping ("/home"
3公共类 Home Captain {
4_
5}
6_
7_(现在/家为URI,将使用此控制器). 这个概念与网络应用程序的 servlet 上下文非常相近.
82. * 请使用方法**: 我们可以用它的方法来提供URI模式,用于操作器方法. 例如:
9QQ
10@ requestMapping(value="/method0")
11@ResponseBody
12公用字符串法 (){
13返回"方法"; (_) MKBR1_)}
14QQAbove 注释也可以被写作" 请求映射 ("/方法0")'. 我用@ResponseBody来为这个网络请求发送字符串响应, 喜欢 我总是这样,我会在Spring MVC应用程序中使用这些方法,用简单的程序或脚本来测试.
153. QQ 以多URI请求映射**:我们可以使用单一的方法处理多URI,例如:
16QQ
17@ request映射 (value_%/method1)", (/method1/second")
18@ResponseBody
19公用字符串方法1 (){
20返回"方法1";
21}
22_ 如果您要查看 ** RequestMapping 注释** 的源代码, 你会看到所有变量都是数组 。 我们可以为处理器方法的URI映射创建字符串阵列.
234. 请求使用 HTTP 方法**: 有时我们想根据所使用的HTTP方法进行不同的操作,尽管请求URI保持不变. 我们可以使用 @ RequestMapping 方法变量来缩小将引用此方法的 HTTP 方法 。 例如:
24QQ
25@请示(价值="/方法2"),方法=请示方法.POST (-MKBR1_) @ResponseBody (-MKBR1_) 公用字符串方法2(){(-MKBR1_) 返回"方法2"; (-MKBR1_)}(-MKBR1_) @请示方法(价值="/方法3") @请示方法=(MKBR1_)_(-MKBR1_)_(-KBRBR1_)_5. * 与信头一起请求**: 我们可以指定应该存在的引用处理器方法的页眉. 例如:
@ requestMapping(value="/method4"),页眉="name=pankaj" @ResponseBody 公用字符串方法4(){ 返回"方法4"; (- )} (- ) (- ) @ requestMapping(value="/方法5",标题+"名=pankaj","id=1" (- ) (-) 公用字符串方法5 (- 返回"方法5"; (- )} (- )_(- ) 6. 请求 与生产者和消费者的映射**:我们可以使用标题 " Content-Type " 和 " Acept " 来了解请求的内容以及它想要的缩影信息。 为了明确起见,@requestMapping提供了produces和concumes变量,我们可以在其中具体说明所请求的内容-类型将使用哪种方法和响应内容类型. 例如: QQ @ requestMapping(value="/method6"), producted QQ"application/json",application/xml"}, clusings="text/html" @ResponseBody 公用字符串方法6 } QAbove方法只能以** Content-Type作为 text/html来收录消息,并且能够生成applicent/json和 applic/xml**
春天 变量
我們甚至可以指定 Regular Expression(/community/tutorials/regular-expression-in-java-regex-example "Java Regular Expression Tutorial with Examples") 以處理 URI 動態參數,以便只接受特定類型的輸入。 它也可指定 Regular Expression(/community/tutorials/regular-expression-in-java-regex-example "Java Regular Expression Tutorial with Examples") 以便接受特定類型的輸入。 它也可使用 Regular Expression(/community/tutorials/regular-expression-in-java-regex-example) 以指定 URI 變量為其中一個方法論數。 例如:( _example) `( _example)
春天请求
10个 请求 用 @ request 绘制地图 URL参数参数参数**: 有时我们在请求的URL中获得参数,大多是在GET请求中. 我们可以使用 @ requestMapping with QQExquestParam 注释** 检索 URL 参数并将其映射到方法参数 。 例如: (-
) (- ) @ 请求映射(值="/方法9")(- ) @ResponseBody (- ) 公用字符串方法9 (@ requestParam ("id") int id (- ) 将"方法9"与"+id; (- )")
(-
) 要使这种方法发挥作用,参数名称应为"id"并应该为t. (_)
)11 (英语). 请求 绘图默认方法**: 如果某方法的值为空,它作为控制器类的默认方法发挥作用. 例如:
1@请示( (_). MKBR1_) @ResponseBody
2 公共字符串默认方法 (){
3 返回"默认方法";
4}
5______ 正如上文所见,我们已经将 " home " 绘制成 " Home Captain " ,这种方法将用于默认的URI请求。 (_ MKBR1_)12 (英语). 请求 绘图回落方法**: 我们可以为控制器类创建一个倒置方法,以确保我们抓住所有客户端请求,尽管没有匹配的处理器方法. 当没有处理请求的方法时,它可以向用户发送自定义的404个响应页。 (_) MKBR1_) QQ
6 @ 请求映射 ("*")
7 @ ResponseBody
8 公有弦回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回回
9
10### 春季测试计划
11
12我們可以使用 [Spring RestTemplate](/community/tutorials/spring-rest-example-tutorial-spring-restful-web-services)來測試上述不同的方法,但今天我會使用cURL命令來測試這些方法,因為這些方法很簡單,並且沒有很多資料流動。
!/bin/bash
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method0"; curl https://localhost:9090/SpringRequestMappingExample/home/method0; printf "\n\n*****\n\n";
echo "curl https://localhost:9090/SpringRequestMappingExample/home"; curl https://localhost:9090/SpringRequestMappingExample/home; printf "\n\n*****\n\n";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/xyz"; curl https://localhost:9090/SpringRequestMappingExample/home/xyz; printf "\n\n*****\n\n";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method1"; curl https://localhost:9090/SpringRequestMappingExample/home/method1; printf "\n\n*****\n\n";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method1/second"; curl https://localhost:9090/SpringRequestMappingExample/home/method1/second; printf "\n\n*****\n\n";
echo "curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method2"; curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method2; printf "\n\n*****\n\n";
echo "curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method3"; curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method3; printf "\n\n*****\n\n";
echo "curl -X GET https://localhost:9090/SpringRequestMappingExample/home/method3"; curl -X GET https://localhost:9090/SpringRequestMappingExample/home/method3; printf "\n\n*****\n\n";
echo "curl -H "name:pankaj" https://localhost:9090/SpringRequestMappingExample/home/method4"; curl -H "name:pankaj" https://localhost:9090/SpringRequestMappingExample/home/method4; printf "\n\n*****\n\n";
echo "curl -H "name:pankaj" -H "id:1" https://localhost:9090/SpringRequestMappingExample/home/method5"; curl -H "name:pankaj" -H "id:1" https://localhost:9090/SpringRequestMappingExample/home/method5; printf "\n\n*****\n\n";
echo "curl -H "Content-Type:text/html" https://localhost:9090/SpringRequestMappingExample/home/method6"; curl -H "Content-Type:text/html" https://localhost:9090/SpringRequestMappingExample/home/method6; printf "\n\n*****\n\n";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method6"; curl https://localhost:9090/SpringRequestMappingExample/home/method6; printf "\n\n*****\n\n";
echo "curl -H "Content-Type:text/html" -H "Accept:application/json" -i https://localhost:9090/SpringRequestMappingExample/home/method6"; curl -H "Content-Type:text/html" -H "Accept:application/json" -i https://localhost:9090/SpringRequestMappingExample/home/method6; printf "\n\n*****\n\n";
echo "curl -H "Content-Type:text/html" -H "Accept:application/xml" -i https://localhost:9090/SpringRequestMappingExample/home/method6"; curl -H "Content-Type:text/html" -H "Accept:application/xml" -i https://localhost:9090/SpringRequestMappingExample/home/method6; printf "\n\n*****\n\n";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method7/1"; curl https://localhost:9090/SpringRequestMappingExample/home/method7/1; printf "\n\n*****\n\n";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa"; curl https://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa; printf "\n\n*****\n\n";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method9?id=20"; curl https://localhost:9090/SpringRequestMappingExample/home/method9?id=20; printf "\n\nDONE\n\n";
1
2请注意,我已经在Tomcat-7上部署了我的Web应用程序,它在端口9090上运行。 **SpringRequestMappingExample**是应用程序的服务器语境。
pankaj:~ pankaj$ ./springTest.sh curl https://localhost:9090/SpringRequestMappingExample/home/method0 method0
curl https://localhost:9090/SpringRequestMappingExample/home default method
curl https://localhost:9090/SpringRequestMappingExample/home/xyz fallback method
curl https://localhost:9090/SpringRequestMappingExample/home/method1 method1
curl https://localhost:9090/SpringRequestMappingExample/home/method1/second method1
curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method2 method2
curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method3 method3
curl -X GET https://localhost:9090/SpringRequestMappingExample/home/method3 method3
curl -H name:pankaj https://localhost:9090/SpringRequestMappingExample/home/method4 method4
curl -H name:pankaj -H id:1 https://localhost:9090/SpringRequestMappingExample/home/method5 method5
curl -H Content-Type:text/html https://localhost:9090/SpringRequestMappingExample/home/method6 method6
curl https://localhost:9090/SpringRequestMappingExample/home/method6 fallback method
curl -H Content-Type:text/html -H Accept:application/json -i https://localhost:9090/SpringRequestMappingExample/home/method6 HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: application/json Content-Length: 7 Date: Thu, 03 Jul 2014 18:14:10 GMT
method6
curl -H Content-Type:text/html -H Accept:application/xml -i https://localhost:9090/SpringRequestMappingExample/home/method6 HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: application/xml Content-Length: 7 Date: Thu, 03 Jul 2014 18:14:10 GMT
method6
curl https://localhost:9090/SpringRequestMappingExample/home/method7/1 method7 with id=1
curl https://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa method8 with id= 10 and name=Lisa
curl https://localhost:9090/SpringRequestMappingExample/home/method9?id=20 method9 with id= 20
DONE
pankaj:~ pankaj$
1
2大多数这些都是自明的,虽然你可能想检查默认和倒退方法. 这就是 **Spring RequestMapping Example**的全部,我希望它会帮助你理解这个注释和它的各种功能。
3
4[下载春季MVC请求绘制项目](https://journaldev.nyc3.digitaloceanspaces.com/spring/SpringRequestMappingExample.zip)