Spring MVC的@RequestMapping注解例子,包含Controller、Methods、Headers、Params、@RequestParam和@PathVariable

@RequestMapping是最广泛使用的Spring MVC注解之一。org.springframework.web.bind.annotation.RequestMapping注解用于将web请求映射到特定的处理类和/或处理方法上。@RequestMapping既可以应用于控制器类,也可以应用于方法。今天我们将通过示例研究这个注解的各种用法,以及其他的注解@PathVariable和@RequestParam。

春天的@RequestMapping

    1. @RequestMapping在类中的使用:我们可以将其与类定义一起使用来创建基本URI。例如:

 

    1. @Controller

 

    1. @RequestMapping(“/home”)

 

    1. public class HomeController {

}

现在/home是将使用该控制器的URI。这个概念与Web应用程序的servlet上下文非常相似。

@RequestMapping与方法一起使用:我们可以将其与方法一起使用,为处理程序方法提供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的method变量来缩小调用该方法的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=scdev”)
@ResponseBody
public String method4(){
return “method4″;
}

@RequestMapping(value=”/method5”, headers={“name=scdev”, “id=1”})
@ResponseBody
public String method5(){
return “method5″;
}

@RequestMapping与Produces和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的@RequestParam

    1. @RequestMapping与@PathVariable:可以使用@RequestMapping注释来处理动态URI,其中一个或多个URI值作为参数工作。我们甚至可以指定URI动态参数的正则表达式,以仅接受特定类型的输入。它与@PathVariable注释一起工作,通过它我们可以将URI变量映射到方法参数之一。例如:

 

    1. @RequestMapping(value=”/method7/{id}”)

 

    1. @ResponseBody

 

    1. public String method7(@PathVariable(“id”) int id){

 

    1. return “method7 with id=”+id;

 

    1. }

@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;
}

@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;
}

春天的@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";
}
```

春季 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:scdev" https://localhost:9090/SpringRequestMappingExample/home/method4";
curl -H "name:scdev" https://localhost:9090/SpringRequestMappingExample/home/method4;
printf "\n\n*****\n\n";

echo "curl -H "name:scdev" -H "id:1" https://localhost:9090/SpringRequestMappingExample/home/method5";
curl -H "name:scdev" -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";

请注意,我已经在Tomcat-7上部署了我的Web应用程序,并且它在9090端口上运行。SpringRequestMappingExample是应用程序的Servlet上下文。现在当我通过命令行执行此脚本时,我得到以下输出。

scdev:~ scdev$ ./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:scdev https://localhost:9090/SpringRequestMappingExample/home/method4
method4

*****

curl -H name:scdev -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*****

scdev:~ scdev$ 

大多数人可以自己理解这些,虽然你可能想要检查默认和备选方法。这就是关于Spring RequestMapping示例的全部内容,希望它对你理解这个注解及其各种特性有所帮助。你应该从下面的链接下载示例项目,尝试不同的场景以进一步探索它。

下载Spring MVC RequestMapping项目

发表回复 0

Your email address will not be published. Required fields are marked *


广告
将在 10 秒后关闭
bannerAds