@RequestMapping是最常用的Spring MVC注解之一。org.springframework.web.bind.annotation.RequestMapping
注解用于将Web请求映射到特定的处理器类和/或处理器方法上。
@RequestMapping
可以应用于控制器类和方法。今天我们将通过示例和其他注解@PathVariable
和@RequestParam
来详细介绍该注解的各种用法。
Spring @RequestMapping
-
@RequestMapping与类一起使用:我们可以将其与类定义一起使用,以创建基本URI。例如:
@Controller @RequestMapping("/home") public class HomeController { }
现在,/home是将使用此控制器的URI。这个概念与Web应用程序的servlet上下文非常相似。
-
@RequestMapping與方法一起使用:我們可以在方法中使用它來提供處理程序方法將被使用的URI模式。例如:
@RequestMapping(value="/method0") @ResponseBody public String method0(){ return "method0"; }
上面的註釋也可以寫成
@RequestMapping("/method0")
。另外一個注意事項是,我正在使用@ResponseBody來發送String型態的回應給這個網路請求,這是為了讓範例簡單。和我以往一樣,我將在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與標頭:我們可以指定應該存在的標頭以調用處理程序方法。例如:
@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 with Produces and Consumes: 我们可以使用标头
Content-Type
和Accept
来确定请求的内容和所需的响应的MIME类型。为了清晰起见,@RequestMapping提供了produces和consumes变量,可以在其中指定调用的方法的请求内容类型和响应内容类型。例如:@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的消息。
Spring @PathVariable
-
@RequestMapping和@PathVariable:RequestMapping注解可以用于处理动态的URI,其中一个或多个URI值可以作为参数。我们甚至可以为URI动态参数指定正则表达式,以接受特定类型的输入。它与@PathVariable注解一起使用,通过它我们可以将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; }
Spring @RequestParam
- @RequestMapping與@RequestParam用於URL參數:有時我們在請求URL中獲取參數,主要是在GET請求中。我們可以使用@RequestMapping與@RequestParam註釋來檢索URL參數並將其映射到方法參數。例如:
```
@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默認方法:如果方法的值為空,則該方法作為控制器類的默認方法。例如:
```
@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回退方法:我們可以為控制器類創建一個回退方法,以確保捕獲所有客戶端請求,即使沒有匹配的處理程序方法。當沒有處理程序方法處理請求時,這對於向用戶發送自定義404響應頁面非常有用。
```
@RequestMapping("*")
@ResponseBody
public String fallbackMethod(){
return "fallback method";
}
```
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 "\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";
請注意,我已經將我的Web應用程序部署在Tomcat-7上,它運行在9090端口上。 SpringRequestMappingExample是應用程序的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: 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$
大部分都是自我理解的,不过你可能想要检查默认和回退方法。以上是有关Spring RequestMapping示例的全部内容,希望能帮助你理解这个注解及其各种特性。你可以从下方链接下载示例项目,并尝试不同的场景来进一步探索。