原文链接:http://www.javaarch.net/jiagoushi/694.htm

  1. spring3 restful API RequestMapping介绍
  2.  
  3. spring mvc @RequestMapping是把web请求映射到controller的方法上。
  4.  
  5. 1.RequestMapping Basic Example
  6. http请求映射到controller方法的最直接方式
  7. 1.1 @RequestMapping by Path
  8.  
  9. @RequestMapping(value = "/foos")
  10. @ResponseBody
  11. public String getFoosBySimplePath() {
  12. return "Get some Foos";
  13. }
  14.  
  15. 可以通过下面的方式测试: curl -i http://localhost:8080/springmvc/foos
  16.  
  17. 1.2 @RequestMapping the HTTP Method,我们可以加上http方法的限制
  18.  
  19. @RequestMapping(value = "/foos", method = RequestMethod.POST)
  20. @ResponseBody
  21. public String postFoos() {
  22. return "Post some Foos";
  23. }
  24.  
  25. 可以通过curl i -X POST http://localhost:8080/springmvc/foos测试。
  26.  
  27. 2.RequestMapping http header
  28.  
  29. 2.1 @RequestMapping with the headers attribute
  30. requestheader包含某个key value值时
  31.  
  32. @RequestMapping(value = "/foos", headers = "key=val")
  33. @ResponseBody
  34. public String getFoosWithHeader() {
  35. return "Get some Foos with Header";
  36. }
  37.  
  38. header多个字段满足条件时
  39.  
  40. @RequestMapping(value = "/foos", headers = { "key1=val1", "key2=val2" })
  41. @ResponseBody
  42. public String getFoosWithHeaders() {
  43. return "Get some Foos with Header";
  44. }
  45.  
  46. 通过curl -i -H "key:val" http://localhost:8080/springmvc/foos 测试。
  47.  
  48. 2.2 @RequestMapping Accept
  49.  
  50. @RequestMapping(value = "/foos", method = RequestMethod.GET, headers = "Accept=application/json")
  51. @ResponseBody
  52. public String getFoosAsJsonFromBrowser() {
  53. return "Get some Foos with Header Old";
  54. }
  55. 支持accept头为json的请求,通过curl -H "Accept:application/json,text/html" http://localhost:8080/springmvc/foos测试
  56.  
  57. spring3.1@RequestMapping注解有produces consumes 两个属性来代替accept
  58.  
  59. @RequestMapping(value = "/foos", method = RequestMethod.GET, produces = "application/json")
  60. @ResponseBody
  61. public String getFoosAsJsonFromREST() {
  62. return "Get some Foos with Header New";
  63. }
  64. 同样可以通过curl -H "Accept:application/json" http://localhost:8080/springmvc/foos测试
  65.  
  66. produces可以支持多个
  67.  
  68. @RequestMapping(value = "/foos", produces = { "application/json", "application/xml" })
  69.  
  70. 当前不能有两个方法同时映射到同一个请求,要不然会出现下面这个异常
  71.  
  72. Caused by: java.lang.IllegalStateException: Ambiguous mapping found.
  73. Cannot map 'fooController' bean method
  74. public java.lang.String org.baeldung.spring.web.controller.FooController.getFoosAsJsonFromREST()
  75. to {[/foos],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}:
  76. There is already 'fooController' bean method
  77. public java.lang.String org.baeldung.spring.web.controller.FooController.getFoosAsJsonFromBrowser()
  78. mapped.
  79.  
  80. 3.RequestMapping with Path Variables
  81. 3.1我们可以把@PathVariableurl映射到controller方法
  82. 单个@PathVariable参数映射
  83.  
  84. @RequestMapping(value = "/foos/{id}")
  85. @ResponseBody
  86. public String getFoosBySimplePathWithPathVariable(@PathVariable("id") long id) {
  87. return "Get a specific Foo with id=" + id;
  88. }
  89.  
  90. 通过curl http://localhost:8080/springmvc/foos/1试试
  91. 如果参数名跟url参数名一样,可以省略为
  92.  
  93. @RequestMapping(value = "/foos/{id}")
  94. @ResponseBody
  95. public String getFoosBySimplePathWithPathVariable(@PathVariable String id) {
  96. return "Get a specific Foo with id=" + id;
  97. }
  98. 3.2 多个@PathVariable
  99.  
  100. @RequestMapping(value = "/foos/{fooid}/bar/{barid}")
  101. @ResponseBody
  102. public String getFoosBySimplePathWithPathVariables(@PathVariable long fooid, @PathVariable long barid) {
  103. return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid;
  104. }
  105.  
  106. 通过curl http://localhost:8080/springmvc/foos/1/bar/2测试。
  107.  
  108. 3.3支持正则的@PathVariable
  109.  
  110. @RequestMapping(value = "/bars/{numericId:[\\d]+}")
  111. @ResponseBody
  112. public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) {
  113. return "Get a specific Bar with id=" + numericId;
  114. }
  115.  
  116. 这个url匹配:http://localhost:8080/springmvc/bars/1
  117. 不过这个不匹配:http://localhost:8080/springmvc/bars/abc
  118.  
  119. 4.RequestMapping with Request Parameters
  120.  
  121. 我们可以使用 @RequestParam注解把请求参数提取出来
  122. 比如urlhttp://localhost:8080/springmvc/bars?id=100
  123.  
  124. @RequestMapping(value = "/bars")
  125. @ResponseBody
  126. public String getBarBySimplePathWithRequestParam(@RequestParam("id") long id) {
  127. return "Get a specific Bar with id=" + id;
  128. }
  129.  
  130. 我们可以通过RequestMapping定义参数列表
  131.  
  132. @RequestMapping(value = "/bars", params = "id")
  133. @ResponseBody
  134. public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") long id) {
  135. return "Get a specific Bar with id=" + id;
  136. }
  137.  
  138.  
  139. @RequestMapping(value = "/bars", params = { "id", "second" })
  140. @ResponseBody
  141. public String getBarBySimplePathWithExplicitRequestParams(@RequestParam("id") long id) {
  142. return "Narrow Get a specific Bar with id=" + id;
  143. }
  144.  
  145. 比如http://localhost:8080/springmvc/bars?id=100&second=something会匹配到最佳匹配的方法上,这里会映射到下面这个。
  146.  
  147. 5.RequestMapping Corner Cases
  148.  
  149. 5.1 @RequestMapping多个路径映射到同一个controller的同一个方法
  150.  
  151. @RequestMapping(value = { "/advanced/bars", "/advanced/foos" })
  152. @ResponseBody
  153. public String getFoosOrBarsByPath() {
  154. return "Advanced - Get some Foos or Bars";
  155. }
  156.  
  157. 下面这两个url会匹配到同一个方法
  158.  
  159. curl -i http://localhost:8080/springmvc/advanced/foos
  160. curl -i http://localhost:8080/springmvc/advanced/bars
  161.  
  162. 5.2@RequestMapping 多个http方法 映射到同一个controller的同一个方法
  163.  
  164. @RequestMapping(value = "/foos/multiple", method = { RequestMethod.PUT, RequestMethod.POST })
  165. @ResponseBody
  166. public String putAndPostFoos() {
  167. return "Advanced - PUT and POST within single method";
  168. }
  169. 下面这两个url都会匹配到上面这个方法
  170.  
  171. curl -i -X POST http://localhost:8080/springmvc/foos/multiple
  172. curl -i -X PUT http://localhost:8080/springmvc/foos/multiple
  173.  
  174. 5.3@RequestMapping 匹配所有方法
  175.  
  176. @RequestMapping(value = "*")
  177. @ResponseBody
  178. public String getFallback() {
  179. return "Fallback for GET Requests";
  180. }
  181.  
  182. 匹配所有方法
  183.  
  184. @RequestMapping(value = "*", method = { RequestMethod.GET, RequestMethod.POST ... })
  185. @ResponseBody
  186. public String allFallback() {
  187. return "Fallback for All Requests";
  188. }
  189.  
  190. 6.Spring Configuration
  191.  
  192. controllerannotation
  193.  
  194. @Controller
  195. public class FooController { ... }
  196.  
  197. spring3.1
  198.  
  199. @Configuration
  200. @EnableWebMvc
  201. @ComponentScan({ "org.baeldung.spring.web.controller" })
  202. public class MvcConfig {
  203. //
  204. }
  205.  
  206. 可以从这里看到所有的示例https://github.com/eugenp/tutorials/tree/master/springmvc

spring3 的restful API RequestMapping介绍的更多相关文章

  1. OpenStack Restful API框架介绍

    1  pecan框架介绍 1.1  什么是pecan pecan是一个轻量级的python web框架,最主要的特点是提供了简单的配置即可创建一个wsgi对象并提供了基于对象的路由方式. 主要提供的功 ...

  2. RESTful api风格介绍

    RESTful 接口是目前来说比较流行的一种接口,平常在开发中会非常常见. 有过和后端人员对接接口的小伙伴都应该知道,我们所做的大多数操作都是对数据库的四格操作 “增删改查” 对应到我们的接口操作分别 ...

  3. kafka restful api功能介绍与使用

    前述 采用confluent kafka-rest proxy实现kafka restful service时候(具体参考上一篇笔记),通过http协议数据传输,需要注意的是采用了base64编码(或 ...

  4. Spring Boot入门系列(二十)快速打造Restful API 接口

    spring boot入门系列文章已经写到第二十篇,前面我们讲了spring boot的基础入门的内容,也介绍了spring boot 整合mybatis,整合redis.整合Thymeleaf 模板 ...

  5. 一文搞懂RESTful API

    RESTful接口实战 原创公众号:bigsai 转载请联系bigsai 文章收藏在回车课堂 前言 在学习RESTful 风格接口之前,即使你不知道它是什么,但你肯定会好奇它能解决什么问题?有什么应用 ...

  6. Spring Boot 2.x 编写 RESTful API (一) RESTful API 介绍 & RestController

    用Spring Boot编写RESTful API 学习笔记 RESTful API 介绍 REST 是 Representational State Transfer 的缩写 所有的东西都是资源,所 ...

  7. python 全栈开发,Day95(RESTful API介绍,基于Django实现RESTful API,DRF 序列化)

    昨日内容回顾 1. rest framework serializer(序列化)的简单使用 QuerySet([ obj, obj, obj]) --> JSON格式数据 0. 安装和导入: p ...

  8. GoldenGate 12.3 MA架构介绍系列(4)–Restful API介绍

    OGG 12.3 MA中最大的变化就是使用了restful api,在前面介绍的各个服务模块,其实就是引用restful api开发而来,这些API同时也提供对外的集成接口,详细接口可参考: http ...

  9. 4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍

    4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍 天涯明月笙 关注 2018.02.20 19:23* 字数 762 阅读 135 ...

随机推荐

  1. 边框(border)边距(margin)和间隙(padding)属性的区别

    边框属性(border):用来设定一个元素的边线.边距属性(margin):用来设置一个元素所占空间的边缘到相邻元素之间的距离.间隙属性(padding):用来设置元素内容到元素边界的距离.这三个属性 ...

  2. C#中(int)a和Convert.ToInt32(a)有什么区别

    首先,在 C# 中,int 其实就是 System.Int32,即都是32位的. 其次,(int) 和 Convert.ToInt32 是两个不同的概念,前者是类型转换,而后者则是内容转换,它们并不总 ...

  3. Storm:最火的流式处理框架

    伴随着信息科技日新月异的发展,信息呈现出爆发式的膨胀,人们获取信息的途径也更加多样.更加便捷,同时对于信息的时效性要求也越来越高.举个搜索场景中的例子,当一个卖家发布了一条宝贝信息时,他希望的当然是这 ...

  4. “Transaction rolled back because it has been marked as rollback-only”

    spring的声明事务提供了强大功能,让我们把业务关注和非业务关注的东西又分离开了.好东西的使用,总是需要有代价的.使用声明事务的时候,一 个不小心经常会碰到“Transaction rolled b ...

  5. MyBatis动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...

  6. UIGestureRecognizer

    •为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer • •利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势 • •UIG ...

  7. 关于高性能Web服务的一点思考

    下面这些概念对于专业做性能测试的会比较熟悉,但是对于开发人员不一定都那么清楚. 并发用户数: 某一时刻同时请求服务器的用户总数,通常我们也称并发数,并发连接数等. 吞吐率:对于web服务器来说就是每秒 ...

  8. Hadoop之Hive 安装_(hadoop 集群)

    Hive mysql的metastore安装准备(***掌握***) 在nameNode1机子上实践: 把hive-0.12.0.tar.gz解压到/itcast/ # tar -zxvf hive- ...

  9. 采用Atlas+Keepalived实现MySQL读写分离、读负载均衡【转载】

      文章 原始出处 :http://sofar.blog.51cto.com/353572/1601552 ============================================== ...

  10. Java之异常处理机制

    来源:深入理解java异常处理机制 2.Java异常    异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.Java通 ...