Spring MVC @RequestMapping Annotation示例
Spring MVC @RequestMapping Annotation示例
Controller,Methods,Headers,Params,@ RequestParam,@ PathVariable
@RequestMapping是最广泛使用的Spring MVC注释之一。org.springframework.web.bind.annotation.RequestMapping
注释用于将Web请求映射到特定的处理程序类和/或处理程序方法。
@RequestMapping
可以应用于控制器类以及方法。今天,我们会考虑这个注释与例子和其他注释的各种使用@PathVariable
和@RequestParam
。
目录[ 隐藏 ]
- 1 Spring @RequestMapping
- 2 Spring @PathVariable
- 3 Spring @RequestParam
- 3.1 Spring RestMapping测试程序
Spring @RequestMapping
Spring @PathVariable
Spring @RequestParam
- @RequestMapping with Class:我们可以将它与类定义一起使用来创建基URI。例如:
@Controller @RequestMapping("/home") public class HomeController {}
现在/ home是将使用此控制器的URI。这个概念与Web应用程序的servlet上下文非常相似。
- @RequestMapping with Method:我们可以使用它与方法一起提供将使用处理程序方法的URI模式。例如:
@RequestMapping(value="/method0") @ResponseBody public String method0(){return "method0"; }
上面的注释也可以写成
@RequestMapping("/method0")
。在旁注中,我使用@ResponseBody发送此Web请求的String响应,这样做是为了使示例简单。像我一直这样,我将在Spring MVC应用程序中使用这些方法,并使用简单的程序或脚本对它们进行测试。 - 带有多个URI的@RequestMapping:我们可以使用单个方法来处理多个URI,例如:
@RequestMapping(value={"/method1","/method1/second"}) @ResponseBody public String method1(){return "method1"; }
如果您将查看RequestMapping注释的源代码,您将看到它的所有变量都是数组。我们可以为处理程序方法的URI映射创建String数组。
- 使用HTTP方法进行@RequestMapping:有时我们希望基于所使用的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:我们可以使用header
Content-Type
并Accept
找出请求内容以及它想要响应的mime消息。为了清楚起见,@RequestMapping提供产生和消耗的变量,我们可以指定请求内容类型方法将被调用针对和响应的内容类型。例如:@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类型的消息。
- @RequestMapping with @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; }
- @RequestMapping使用@RequestParam获取URL参数:有时我们在请求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
到的HomeController
,此方法将用于默认的URI请求。 - @RequestMapping回退方法:我们可以为控制器类创建一个回退方法,以确保我们捕获所有客户端请求,即使没有匹配的处理程序方法。当没有请求的处理程序方法时,它可以向用户发送自定义404响应页面。
@RequestMapping("*") @ResponseBody public String fallbackMethod(){return "fallback method"; }
Spring RestMapping测试程序
我们可以使用Spring RestTemplate来测试上面的不同方法,但今天我将使用cURL命令来测试这些方法,因为这些方法很简单并且没有太多数据流动。
我创建了一个简单的shell脚本springTest.sh来调用所有上述方法并打印它们的输出。它看起来像下面。
#!/bin/bashecho "curl http://localhost:9090/SpringRequestMappingExample/home/method0";
curl http://localhost:9090/SpringRequestMappingExample/home/method0;
printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home";
curl http://localhost:9090/SpringRequestMappingExample/home;
printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/xyz";
curl http://localhost:9090/SpringRequestMappingExample/home/xyz;
printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method1";
curl http://localhost:9090/SpringRequestMappingExample/home/method1;
printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method1/second";
curl http://localhost:9090/SpringRequestMappingExample/home/method1/second;
printf "\n\n*****\n\n";echo "curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method2";
curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method2;
printf "\n\n*****\n\n";echo "curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method3";
curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method3;
printf "\n\n*****\n\n";echo "curl -X GET http://localhost:9090/SpringRequestMappingExample/home/method3";
curl -X GET http://localhost:9090/SpringRequestMappingExample/home/method3;
printf "\n\n*****\n\n";echo "curl -H "name:pankaj" http://localhost:9090/SpringRequestMappingExample/home/method4";
curl -H "name:pankaj" http://localhost:9090/SpringRequestMappingExample/home/method4;
printf "\n\n*****\n\n";echo "curl -H "name:pankaj" -H "id:1" http://localhost:9090/SpringRequestMappingExample/home/method5";
curl -H "name:pankaj" -H "id:1" http://localhost:9090/SpringRequestMappingExample/home/method5;
printf "\n\n*****\n\n";echo "curl -H "Content-Type:text/html" http://localhost:9090/SpringRequestMappingExample/home/method6";
curl -H "Content-Type:text/html" http://localhost:9090/SpringRequestMappingExample/home/method6;
printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method6";
curl http://localhost:9090/SpringRequestMappingExample/home/method6;
printf "\n\n*****\n\n";echo "curl -H "Content-Type:text/html" -H "Accept:application/json" -i http://localhost:9090/SpringRequestMappingExample/home/method6";
curl -H "Content-Type:text/html" -H "Accept:application/json" -i http://localhost:9090/SpringRequestMappingExample/home/method6;
printf "\n\n*****\n\n";echo "curl -H "Content-Type:text/html" -H "Accept:application/xml" -i http://localhost:9090/SpringRequestMappingExample/home/method6";
curl -H "Content-Type:text/html" -H "Accept:application/xml" -i http://localhost:9090/SpringRequestMappingExample/home/method6;
printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method7/1";
curl http://localhost:9090/SpringRequestMappingExample/home/method7/1;
printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa";
curl http://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa;
printf "\n\n*****\n\n";echo "curl http://localhost:9090/SpringRequestMappingExample/home/method9?id=20";
curl http://localhost:9090/SpringRequestMappingExample/home/method9?id=20;
printf "\n\n*****DONE*****\n\n";
请注意,我已在Tomcat-7上部署了我的Web应用程序,并且它在端口9090上运行.SpringRequestMappingExample是应用程序的servlet上下文。现在,当我通过命令行执行此脚本时,我得到以下输出。
pankaj:~ pankaj$ ./springTest.sh
curl http://localhost:9090/SpringRequestMappingExample/home/method0
method0*****curl http://localhost:9090/SpringRequestMappingExample/home
default method*****curl http://localhost:9090/SpringRequestMappingExample/home/xyz
fallback method*****curl http://localhost:9090/SpringRequestMappingExample/home/method1
method1*****curl http://localhost:9090/SpringRequestMappingExample/home/method1/second
method1*****curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method2
method2*****curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method3
method3*****curl -X GET http://localhost:9090/SpringRequestMappingExample/home/method3
method3*****curl -H name:pankaj http://localhost:9090/SpringRequestMappingExample/home/method4
method4*****curl -H name:pankaj -H id:1 http://localhost:9090/SpringRequestMappingExample/home/method5
method5*****curl -H Content-Type:text/html http://localhost:9090/SpringRequestMappingExample/home/method6
method6*****curl http://localhost:9090/SpringRequestMappingExample/home/method6
fallback method*****curl -H Content-Type:text/html -H Accept:application/json -i http://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 GMTmethod6*****curl -H Content-Type:text/html -H Accept:application/xml -i http://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 GMTmethod6*****curl http://localhost:9090/SpringRequestMappingExample/home/method7/1
method7 with id=1*****curl http://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa
method8 with id= 10 and name=Lisa*****curl http://localhost:9090/SpringRequestMappingExample/home/method9?id=20
method9 with id= 20*****DONE*****pankaj:~ pankaj$
尽管您可能想要检查默认和回退方法,但大多数都是自我理解的。这就是Spring RequestMapping示例,我希望它能帮助您理解这个注释以及它的各种功能。您应该从下面的链接下载示例项目,并尝试不同的方案来进一步探索它。
下载Spring MVC RequestMapping项目
转载来源:https://www.journaldev.com/3358/spring-requestmapping-requestparam-pathvariable-example
Spring MVC @RequestMapping Annotation示例相关推荐
- 带有控制器,方法,标题,参数,@ RequestParam,@ PathVariable的Spring MVC @RequestMapping注释示例
@RequestMapping is one of the most widely used Spring MVC annotation. org.springframework.web.bind.a ...
- Spring MVC @RequestMapping详解
Spring MVC @RequestMapping RequestMapping Spring MVC @RequestMapping 1. RequestMapping 概念 2. Request ...
- Spring MVC - Hello World示例
以下示例演示如何使用Spring MVC框架编写一个简单的基于Web的Hello World应用程序.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发一个 ...
- Google App Engine + JDO + Spring MVC,CRUD示例
注意 本教程更多关于实践指南,请参考此官方的在数据存储区中使用JDO了解详细说明. 请参阅以下代码段,以使用Java数据对象(JDO)在GAE数据存储上执行CRUD. 只需使用JDO注释为客户注释,然 ...
- spring mvc -@RequestMapping注解详解
https://www.cnblogs.com/caoyc/p/5635173.html @RequestMapping参数说明: value:定义处理方法的请求的URL地址(重点): method: ...
- Spring MVC @RequestMapping注解详解
@RequestMapping 参数说明 value:定义处理方法的请求的 URL 地址.(重点) method:定义处理方法的 http method 类型,如 GET.POST 等.(重点) pa ...
- Spring MVC错误处理示例
这篇文章描述了在Spring MVC 3中执行错误处理的不同技术.该代码在GitHub上的Spring-MVC-Error-Handling目录中可用. 它基于带有注释的Spring MVC示例. 在 ...
- 关于spring MVC机制,示例解读
2019独角兽企业重金招聘Python工程师标准>>> spring MVC分离了控制器.模型对象.工作过程如下: 1.Spring mvc请所有的请求都提交给DispatcherS ...
- Spring MVC拦截器示例
我以为是时候看看Spring的MVC拦截器机制了,这种机制已经存在了很多年,并且是一个非常有用的工具. Spring Interceptor会按照提示进行操作:在传入的HTTP请求到达您的Spring ...
最新文章
- android 按住拖动gallery防止马上加载数据导致gallery卡的方法
- python多项分时求和_python实现连续变量最优分箱详解--CART算法
- jmeter脚本_性能工具之Jmeter脚本python启动
- 【STM32】FSMC相关函数和类型
- python3 类的一个实例
- 树莓派3显示服务器SSH拒绝了密码,脚本封杀尝试树莓派SSH密码的来源IP
- 网络摄像头转usb接口_Arduino + USB Host Sheild 实现USB鼠标转PS/2接口
- hadoop 自定义数据类型
- ArcEngine中shp中先加入要素然后删除一部分要素后,放大图形后不显示的问题解决方法...
- java编辑遗忘曲线代码_用jsp写出记忆曲线的表格(用学习新概念英语做例子)
- eclipse导入wsdl文件_Eclipse+Axis使用WSDL文件生成Web Service服务端/客户端
- html form表格采购单,【HTML】--- 列表、表格、form表单标签
- PR界面基础介绍与应用
- app通过电商变现方式探讨
- 常见的弱口令爆破工具
- 为什么游戏需要热更新?
- HTML学习07(实践1):HTML常用标签之标题、段落、换行、空格、列表
- linux php环境搭建教程,linux php环境搭建教程
- android 全键盘手机排行榜,提升打字效率 全键盘Android手机盘点
- 企业月结快递管理教程
热门文章
- iOS7应用开发1、菜鸟那点儿事儿
- click() bind() live() delegate()区别
- 正则表达式 去除连续空白 + 获取url +分割url + 获取图片
- jquery csv2table 插件
- Leetcode算法题(C语言)6--只出现一次的数字
- 前端开发 页面跳转练习 0228未完成
- 191202-GETJOB-捡历的写法
- python-面向对向-静态方法的继承-父类中的super方法
- linux-权限操作,数字法
- CMD(命令提示符)-------javac编译程序出现“”编码GBK的不可映射字符“”