Spring MVC @RequestMapping 注释示例,带有控制器,方法,标题,参数,@RequestParam,@PathVariable
@RequestMapping是最广泛使用的Spring MVC注释之一。org.springframework.web.bind.annotation.RequestMapping
注释用于将 Web 请求映射到特定的处理程序类和/或处理程序方法。,我们将通过示例和其他注释来研究此注释的各种用法。 @RequestMapping
@PathVariable
@RequestParam
春季@RequestMapping
-
@RequestMapping和 Class : 我们可以使用它和类定义来创建基本 URI。例如:
@Controller @RequestMapping("/home") public class HomeController { }
现在 /home 是此控制器将使用的 URI。此概念与 Web 应用程序的 servlet 上下文非常相似。
-
@RequestMapping和 Method : 我们可以将其与方法一起使用,以提供将使用哪个处理程序方法的 URI 模式。例如:
@RequestMapping(value="/method0") @ResponseBody public String method0(){ return "method0"; }
上述注释也可以写成
@RequestMapping("/method0")
。顺便提一下,我使用@ResponseBody发送此 Web 请求的字符串响应,这样做是为了让示例保持简单。像往常一样,我将在 Spring MVC 应用程序中使用这些方法,并使用简单的程序或脚本对其进行测试。 -
@RequestMapping具有多个 URI:我们可以使用单一方法来处理多个 URI,例如:
@RequestMapping(value={"/method1","/method1/second"}) @ResponseBody public String method1(){ return "method1"; }
如果您查看RequestMapping 注释的源代码,您会发现它的所有变量都是数组。我们可以为处理程序方法的 URI 映射创建字符串数组。
-
@RequestMapping和 HTTP 方法:有时我们希望根据所使用的 HTTP 方法执行不同的操作,即使请求 URI 保持不变。我们可以使用@RequestMapping方法变量来缩小将调用此方法的 HTTP 方法的范围。例如:
@RequestMapping(value="/method2", method=RequestMethod.POST) @ResponseBody public String method2(){ return "method2"; } @RequestMapping(value="/method3", method={RequestMethod.POST,RequestMethod.GET}) @ResponseBody public String method3(){ return "method3"; }
-
@RequestMapping带有 Headers:我们可以指定应该存在的标头来调用处理程序方法。例如:
@RequestMapping(value="/method4", headers="name=pankaj") @ResponseBody public String method4(){ return "method4"; } @RequestMapping(value="/method5", headers={"name=pankaj", "id=1"}) @ResponseBody public String method5(){ return "method5"; }
-
@RequestMapping带有 Produces 和 Consumes:我们可以使用标头
Content-Type
和Accept
查找请求内容以及它希望在响应中显示的 mime 消息。为了清楚起见, @RequestMapping提供了produce和consumer变量,我们可以在其中指定将调用哪种方法的请求内容类型以及响应内容类型。例如:@RequestMapping(value="/method6", produces={"application/json","application/xml"}, consumes="text/html") @ResponseBody public String method6(){ return "method6"; }
上述方法只能使用Content-Type 为 text/html 的消息,并且能够生成application/json和application/xml类型的消息。
春季@PathVariable
-
@RequestMapping with @PathVariable: RequestMapping annotation can be used to handle dynamic URIs where one or more of the URI value works as a parameter. We can even specify Regular Expression for URI dynamic parameter to accept only specific type of input. It works with @PathVariable annotation through which we can map the URI variable to one of the method arguments. For example:
@RequestMapping(value="/method7/{id}") @ResponseBody public String method7(@PathVariable("id") int id){ return "method7 with id="+id; } @RequestMapping(value="/method8/{id:[\\d]+}/{name}") @ResponseBody public String method8(@PathVariable("id") long id, @PathVariable("name") String name){ return "method8 with id= "+id+" and name="+name; }
Spring @RequestParam
- @RequestMapping with @RequestParam for URL parameters: Sometimes we get parameters in the request URL, mostly in GET requests. We can use @RequestMapping with @RequestParam annotation to retrieve the URL parameter and map it to the method argument. For example:
```
@RequestMapping(value="/method9")
@ResponseBody
public 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.
- @RequestMapping default method: If value is empty for a method, it works as default method for the controller class. For example:
```
@RequestMapping()
@ResponseBody
public 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.
- @RequestMapping fallback method: We can create a fallback method for the controller class to make sure we are catching all the client requests even though there are no matching handler methods. It is useful in sending custom 404 response pages to users when there are no handler methods for the request.
```
@RequestMapping("*")
@ResponseBody
public String fallbackMethod(){
return "fallback method";
}
```
Spring RestMapping Test Program
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 springTest.sh to invoke all the above methods and print their output. It looks like below.
#!/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\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.
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$
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.
Download Spring MVC RequestMapping Project