@请求映射是应用最广泛的SpringMVC注解。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
.
Spring@RequestMapping
- @类的请求映射:我们可以将它与类定义一起使用,以创建基URI。例如:
@Controller @RequestMapping("/home") public class HomeController { }
现在/home是这个控制器将用于的URI。这个概念非常类似于web应用程序的servlet上下文。
- @方法的请求映射:我们可以将它与方法一起使用,以提供将使用处理程序方法的URI模式。例如:
@RequestMapping(value="/method0") @ResponseBody public String method0(){ return "method0"; }
上述注解也可以写成
@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. - @具有多个URI的请求映射:我们可以使用单个方法来处理多个uri,例如:
@RequestMapping(value={"/method1","/method1/second"}) @ResponseBody public String method1(){ return "method1"; }
如果你看看RequestMapping注解,您将看到它的所有变量都是数组。我们可以为处理程序方法的URI映射创建字符串数组。
- @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(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"; }
- @带有生产和消费的请求映射:我们可以使用标题
Content-Type
andAccept
to find out request contents and what is the mime message it wants in response. For clarity, @RequestMapping provides 生产和消费变量,其中我们可以指定将为其调用方法的请求内容类型和响应内容类型。例如:@RequestMapping(value="/method6", produces={"application/json","application/xml"}, consumes="text/html") @ResponseBody public String method6(){ return "method6"; }
以上方法只能使用内容类型为text/html并且能够生成类型的消息应用程序/json和应用程序/xml.
- @使用@PathVariable进行请求映射:RequestMapping注解可用于处理动态URI,其中一个或多个URI值用作参数。我们甚至可以指定正则表达式使URI动态参数只接受特定类型的输入。它与@路径变量注解通过它我们可以将URI变量映射到其中一个方法参数。例如:
@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; }
- @使用@RequestParam对URL参数进行RequestMapping:有时我们在请求URL中获取参数,主要是在get请求中。我们可以使用@RequestMapping@RequestParam注解检索URL参数并将其映射到方法参数。例如:
@RequestMapping(value="/method9") @ResponseBody public String method9(@RequestParam("id") int id){ return "method9 with id= "+id; }
要使此方法正常工作,参数名应为“;id”;,并且应为int类型。
- @RequestMapping默认方法:如果某个方法的值为空,则它将作为控制器类的默认方法。例如:
@RequestMapping() @ResponseBody public String defaultMethod(){ return "default method"; }
正如你在上面看到的,我们已经绘制了地图
/home
toHomeController
, this method will be used for the default URI requests. - @请求映射回退方法:我们可以为控制器类创建一个回退方法,以确保即使没有匹配的处理程序方法,也能捕获所有客户端请求。当请求没有处理程序方法时,向用户发送自定义404响应页非常有用。
@RequestMapping("*") @ResponseBody public String fallbackMethod(){ return "fallback method"; }
Spring@路径变量
Spring@RequestParam
Spring RestMapping测试程序
我们可以利用Spring RestTemplate为了测试上面的不同方法,但是今天我将使用cURL命令来测试这些方法,因为这些方法很简单,并且没有太多的数据流动。
我创建了一个简单的shell脚本springTest.sh版调用上述所有方法并打印其输出。它看起来像下面。
#!/bin/bash
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method0";
curl https://localhost:9090/SpringRequestMappingExample/home/method0;
printf "
*****
";
echo "curl https://localhost:9090/SpringRequestMappingExample/home";
curl https://localhost:9090/SpringRequestMappingExample/home;
printf "
*****
";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/xyz";
curl https://localhost:9090/SpringRequestMappingExample/home/xyz;
printf "
*****
";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method1";
curl https://localhost:9090/SpringRequestMappingExample/home/method1;
printf "
*****
";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method1/second";
curl https://localhost:9090/SpringRequestMappingExample/home/method1/second;
printf "
*****
";
echo "curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method2";
curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method2;
printf "
*****
";
echo "curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method3";
curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method3;
printf "
*****
";
echo "curl -X GET https://localhost:9090/SpringRequestMappingExample/home/method3";
curl -X GET https://localhost:9090/SpringRequestMappingExample/home/method3;
printf "
*****
";
echo "curl -H "name:pankaj" https://localhost:9090/SpringRequestMappingExample/home/method4";
curl -H "name:pankaj" https://localhost:9090/SpringRequestMappingExample/home/method4;
printf "
*****
";
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 "
*****
";
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 "
*****
";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method6";
curl https://localhost:9090/SpringRequestMappingExample/home/method6;
printf "
*****
";
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 "
*****
";
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 "
*****
";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method7/1";
curl https://localhost:9090/SpringRequestMappingExample/home/method7/1;
printf "
*****
";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa";
curl https://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa;
printf "
*****
";
echo "curl https://localhost:9090/SpringRequestMappingExample/home/method9?id=20";
curl https://localhost:9090/SpringRequestMappingExample/home/method9?id=20;
printf "
*****DONE*****
";
请注意,我已经在Tomcat-7上部署了web应用程序,它运行在端口9090上。SpringRequestMapping示例是应用程序的servlet上下文。现在,当我通过命令行执行这个脚本时,我得到以下输出。
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: 阿帕奇-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: 阿帕奇-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$
尽管您可能需要检查default和fallback方法,但大多数都是可以自行理解的。那是为了Spring请求映射示例,我希望它能帮助您理解这个注解以及它的各种特性。您应该从下面的链接下载示例项目,并尝试不同的场景来进一步研究它。