标签:
原文地址:
@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.
|
1
2
3
4
5
|
@Controller@RequestMapping("/home")public class HomeController {} |
Now /home is the URI for which this controller will be used. This concept is very similar to servlet context of a web application.
|
1
2
3
4
5
|
@RequestMapping(value="/method0")@ResponseBodypublic String method0(){ return "method0";} |
Above annotation can also be written as @RequestMapping("/method0"). On a side note, I am using @ResponseBody to send the String response for this web request, this is done to keep the example simple. Like I always do, I will use these methods in Spring MVC application and test them with a simple program or script.
|
1
2
3
4
5
|
@RequestMapping(value={"/method1","/method1/second"})@ResponseBodypublic String method1(){ return "method1";} |
If you will look at the source code of RequestMapping annotation, you will see that all of it’s variables are arrays. We can create String array for the URI mappings for the handler method.
|
1
2
3
4
5
6
7
8
9
10
11
|
@RequestMapping(value="/method2", method=RequestMethod.POST)@ResponseBodypublic String method2(){ return "method2";}@RequestMapping(value="/method3", method={RequestMethod.POST,RequestMethod.GET})@ResponseBodypublic String method3(){ return "method3";} |
|
1
2
3
4
5
6
7
8
9
10
11
|
@RequestMapping(value="/method4", headers="name=pankaj")@ResponseBodypublic String method4(){ return "method4";}@RequestMapping(value="/method5", headers={"name=pankaj", "id=1"})@ResponseBodypublic String method5(){ return "method5";} |
Content-Type and Accept to find out request contents and what is the mime message it wants in response. For clarity, @RequestMapping provides produces and consumes variables where we can specify the request content-type for which method will be invoked and the response content type. For example:
|
1
2
3
4
5
|
@RequestMapping(value="/method6", produces={"application/json","application/xml"}, consumes="text/html")@ResponseBodypublic String method6(){ return "method6";} |
Above method can consume message only with Content-Type as text/html and is able to produce messages of type application/json and application/xml.
|
1
2
3
4
5
6
7
8
9
10
11
|
@RequestMapping(value="/method7/{id}")@ResponseBodypublic String method7(@PathVariable("id") int id){ return "method7 with id="+id;}@RequestMapping(value="/method8/{id:[\\d]+}/{name}")@ResponseBodypublic String method8(@PathVariable("id") long id, @PathVariable("name") String name){ return "method8 with id= "+id+" and name="+name;} |
|
1
2
3
4
5
|
@RequestMapping(value="/method9")@ResponseBodypublic String method9(@RequestParam("id") int id){ return "method9 with id= "+id;} |
For this method to work, the parameter name should be “id” and it should be of type int.
|
1
2
3
4
5
|
@RequestMapping()@ResponseBodypublic String defaultMethod(){ return "default method";} |
As you have seen above that we have mapped /home to HomeController, this method will be used for the default URI requests.
|
1
2
3
4
5
|
@RequestMapping("*")@ResponseBodypublic String fallbackMethod(){ return "fallback method";} |
We can use Spring RestTemplate to test the different methods above, but today I will use cURL commands to test these methods because these are simple and there are not much data flowing around.
I have created a simple shell script to invoke all the above methods and print their output. It looks like below.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#!/bin/bashecho "curl http://localhost:9090/SpringRequestMappingExample/home/method0";curl http://localhost:9090/SpringRequestMappingExample/home/method0;printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home";curl http://localhost:9090/SpringRequestMappingExample/home;printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/xyz";curl http://localhost:9090/SpringRequestMappingExample/home/xyz;printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method1";curl http://localhost:9090/SpringRequestMappingExample/home/method1;printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method1/second";curl http://localhost:9090/SpringRequestMappingExample/home/method1/second;printf "\n\n*****\n\n";echo "curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method2";curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method2;printf "\n\n*****\n\n";echo "curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method3";curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method3;printf "\n\n*****\n\n";echo "curl -X GET http://localhost:9090/SpringRequestMappingExample/home/method3";curl -X GET http://localhost:9090/SpringRequestMappingExample/home/method3;printf "\n\n*****\n\n";echo "curl -H "name:pankaj" http://localhost:9090/SpringRequestMappingExample/home/method4";curl -H "name:pankaj" http://localhost:9090/SpringRequestMappingExample/home/method4;printf "\n\n*****\n\n";echo "curl -H "name:pankaj" -H "id:1" http://localhost:9090/SpringRequestMappingExample/home/method5";curl -H "name:pankaj" -H "id:1" http://localhost:9090/SpringRequestMappingExample/home/method5;printf "\n\n*****\n\n";echo "curl -H "Content-Type:text/html" http://localhost:9090/SpringRequestMappingExample/home/method6";curl -H "Content-Type:text/html" http://localhost:9090/SpringRequestMappingExample/home/method6;printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method6";curl http://localhost:9090/SpringRequestMappingExample/home/method6;printf "\n\n*****\n\n";echo "curl -H "Content-Type:text/html" -H "Accept:application/json" -i http://localhost:9090/SpringRequestMappingExample/home/method6";curl -H "Content-Type:text/html" -H "Accept:application/json" -i http://localhost:9090/SpringRequestMappingExample/home/method6;printf "\n\n*****\n\n";echo "curl -H "Content-Type:text/html" -H "Accept:application/xml" -i http://localhost:9090/SpringRequestMappingExample/home/method6";curl -H "Content-Type:text/html" -H "Accept:application/xml" -i http://localhost:9090/SpringRequestMappingExample/home/method6;printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method7/1";curl http://localhost:9090/SpringRequestMappingExample/home/method7/1;printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa";curl http://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa;printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method9?id=20";curl http://localhost:9090/SpringRequestMappingExample/home/method9?id=20;printf "\n\n*****DONE*****\n\n"; |
Note that I have deployed my web application on Tomcat-7 and it’s running on port 9090.SpringRequestMappingExample is the servlet context of the application. Now when I execute this script through command line, I get following output.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
pankaj:~ pankaj$ ./springTest.sh curl http://localhost:9090/SpringRequestMappingExample/home/method0method0*****curl http://localhost:9090/SpringRequestMappingExample/homedefault method*****curl http://localhost:9090/SpringRequestMappingExample/home/xyzfallback method*****curl http://localhost:9090/SpringRequestMappingExample/home/method1method1*****curl http://localhost:9090/SpringRequestMappingExample/home/method1/secondmethod1*****curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method2method2*****curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method3method3*****curl -X GET http://localhost:9090/SpringRequestMappingExample/home/method3method3*****curl -H name:pankaj http://localhost:9090/SpringRequestMappingExample/home/method4method4*****curl -H name:pankaj -H id:1 http://localhost:9090/SpringRequestMappingExample/home/method5method5*****curl -H Content-Type:text/html http://localhost:9090/SpringRequestMappingExample/home/method6method6*****curl http://localhost:9090/SpringRequestMappingExample/home/method6fallback method*****curl -H Content-Type:text/html -H Accept:application/json -i http://localhost:9090/SpringRequestMappingExample/home/method6HTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Type: application/jsonContent-Length: 7Date: Thu, 03 Jul 2014 18:14:10 GMTmethod6*****curl -H Content-Type:text/html -H Accept:application/xml -i http://localhost:9090/SpringRequestMappingExample/home/method6HTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Type: application/xmlContent-Length: 7Date: Thu, 03 Jul 2014 18:14:10 GMTmethod6*****curl http://localhost:9090/SpringRequestMappingExample/home/method7/1method7 with id=1*****curl http://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisamethod8 with id= 10 and name=Lisa*****curl http://localhost:9090/SpringRequestMappingExample/home/method9?id=20method9 with id= 20*****DONE*****pankaj:~ pankaj$ |
Most of these are self understood, although you might want to check default and fallback methods. That’s all for Spring RequestMapping Example, I hope it will help you in understanding this annotation and it’s various features. You should download the sample project from below link and try different scenarios to explore it further.
标签:
原文地址:http://www.cnblogs.com/davidwang456/p/4501664.html