Spring MVC @RequestMapping注解示例,包括Controller、Methods、Headers、Params、@RequestParam、@PathVariable

@RequestMapping是最常用的Spring MVC注解之一。 org.springframework.web.bind.annotation.RequestMapping注解用于将web请求映射到特定的处理程序类和/或处理程序方法。 @RequestMapping可以应用于控制器类以及方法。 今天我们将通过示例和其他注解@PathVariable@RequestParam来探讨此注解的各种用法。

Spring @RequestMapping

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

    @Controller
    @RequestMapping("/home")
    public class HomeController {
    
    }
    

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

  2. @RequestMapping与方法联用:我们可以在方法定义时使用它来提供处理器方法将被使用的URI模式。例如:

    @RequestMapping(value="/method0")
    @ResponseBody
    public String method0(){
            return "method0";
    }
    

    以上注释也可以写成@RequestMapping("/method0")。另外,我在这里使用@ResponseBody来发送该web请求的字符串响应,这是为了保持示例的简单性。像我平常做的一样,我会在Spring MVC应用中使用这些方法,并使用一个简单的程序或脚本来测试它们。

  3. @RequestMapping 处理多个URI: 我们可以使用单个方法来处理多个URI,例如:

    @RequestMapping(value={"/method1","/method1/second"})
    @ResponseBody
    public String method1(){
    	return "method1";
    }
    

    如果你查看 RequestMapping注解 的源代码,你会看到它的所有变量都是数组。我们可以为处理程序方法创建URI映射的字符串数组。

  4. @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";
    }
    
  5. @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";
    }
    
  6. @RequestMapping 带 Produces 和 Consumes:我们可以使用标头 Content-TypeAccept 来查找请求内容以及它希望在响应中得到的 MIME 消息。为了清晰起见,@RequestMapping 提供了 producesconsumes 变量,我们可以在其中指定调用方法的请求内容类型和响应内容类型。例如:

    @RequestMapping(value="/method6", produces={"application/json","application/xml"}, consumes="text/html")
    @ResponseBody
    public String method6(){
    	return "method6";
    }
    

    上述方法只能消费带有 Content-Type 为 text/html 的消息,并能够生成类型为 application/jsonapplication/xml 的消息。

Spring @PathVariable

  1. @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

  1. @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.
  1. @RequestMapping 默认方法:如果方法的value为空,它将作为控制器类的默认方法。例如:
```
@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.
  1. @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";

注意,我已经在Tomcat-7上部署了我的web应用程序,并且它在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 示例的全部内容,希望它能帮助您理解这个注解及其各种特性。您应该从下面的链接下载示例项目,并尝试不同的场景来进一步探索它。

下载 Spring MVC RequestMapping 项目

Source:
https://www.digitalocean.com/community/tutorials/spring-requestmapping-requestparam-pathvariable-example