2 consumes、produces 示例

cousumes的样例:

1 @Controller  
2 @RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")  
3 public void addPet(@RequestBody Pet pet, Model model) {      
4     // implementation omitted  
5 }  

方法仅处理request Content-Type为“application/json”类型的请求。

produces的样例:

1 @Controller  
2 @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")  
3 @ResponseBody  
4 public Pet getPet(@PathVariable String petId, Model model) {      
5     // implementation omitted  
6 }  

方法仅处理request请求中Accept头中包含了"application/json"的请求,同时暗示了返回的内容类型为application/json;

3 params、headers 示例

params的样例:

1 @Controller  
2 @RequestMapping("/owners/{ownerId}")  
3 public class RelativePathUriTemplateController {  
4   
5   @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue")  
6   public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {      
7     // implementation omitted  
8   }  
9 }  

仅处理请求中包含了名为“myParam”,值为“myValue”的请求;

headers的样例:

1 @Controller  
2 @RequestMapping("/owners/{ownerId}")  
3 public class RelativePathUriTemplateController {  
4   
5 @RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/")  
6   public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {      
7     // implementation omitted  
8   }  
9 }  

仅处理request的header中包含了指定“Refer”请求头和对应值为“http://www.ifeng.com/”的请求;

简介:

handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型)

A、处理requet uri 部分(这里指uri template中variable,不含queryString部分)的注解:   @PathVariable;

B、处理request header部分的注解:   @RequestHeader, @CookieValue;

C、处理request body部分的注解:@RequestParam,  @RequestBody;

D、处理attribute类型是注解: @SessionAttributes, @ModelAttribute;

1、 @PathVariable

当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。

示例代码:

  1. @Controller
  2. @RequestMapping("/owners/{ownerId}")
  3. public class RelativePathUriTemplateController {
  4. @RequestMapping("/pets/{petId}")
  5. public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
  6. // implementation omitted
  7. }
  8. }

上面代码把URI template 中变量 ownerId的值和petId的值,绑定到方法的参数上。若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称。

SpringMvc之参数绑定注解详解之二的更多相关文章

  1. SpringMvc之参数绑定注解详解之三

    2. @RequestHeader.@CookieValue @RequestHeader 注解,可以把Request请求header部分的值绑定到方法的参数上. 示例代码: 这是一个Request ...

  2. SpringMvc之参数绑定注解详解之一

    引言: 前段时间项目中用到了REST风格来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加 任何注解),查看了提交方式为application/ ...

  3. SpringMvc之参数绑定注解详解

    引言: 前段时间项目中用到了REST风格来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/j ...

  4. SpringMvc之参数绑定注解详解之四

    简介: @RequestBody 作用: i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对 ...

  5. 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    @RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...

  6. @PathVariable @RequestParam @RequestBody等参数绑定注解详解

    一.分类 handler method 参数绑定常用的注解,我们根据他们处理的Request的内容不同分为四类: 处理request uri 部分的注解:   @PathVariable;(这里指ur ...

  7. SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解

    request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: http://blog.csdn.net/walkerjong/article/details/794 ...

  8. springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...

  9. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...

随机推荐

  1. Android TextView文字过多时通过滚动条显示多余内容

    方法一: TextView文字过多,显示不全,怎么办?我们可以为Textview添加滚动条. <TextView android:id="@+id/bus_detail_content ...

  2. SUBMIT 用法

    [转自http://lz357502668.blog.163.com/blog/static/16496743201241195817597/] 1.最普通的用法 *Code used to exec ...

  3. R语言数据管理(四):数据导出

    与read.*函数对应,导出函数为write.*函数. 比较常见的为write.csv和write.table. 一般格式: setwd("D:\\") write.table(y ...

  4. 配置asp.net IIS服务器遇到的问题以及详细图例解决方案

    原来配置过IIS服务,但是没有将自己的程序放在IIS服务器上,今天突发奇想,看看自己写的程序怎么样啊,能否在服务器上运行一下,说干就干,但是遇到了很多困难,今天把陪孩子过程以及遇到的问题给大家说说,希 ...

  5. 解决pod没有权限问题

    chmod 644 路径 echo $? 检测上一条命令的执行结果,如果是0则执行成功

  6. python 3 模块

    python3 模块 一 .模块 1 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. import加载的模块分为四个通用类别: 1 ...

  7. jquery 初篇

    一.什么是jQuery对象? jQuery 对象就是通过jQuery包装DOM对象后产生的对象. jQuery 对象是 jQuery 独有的. 如果一个对象是 jQuery 对象, 那么它就可以使用  ...

  8. js里对php存贮的cookie进行读取和删除

    /* 读取cookie */ function getCookie(name){ var arr,reg=new RegExp("(^| )"+name+"=([^;]* ...

  9. Codeforces 478D Red-Green Towers:dp

    题目链接:http://codeforces.com/problemset/problem/478/D 题意: 给你r个红方块和g个绿方块,让你用这些方块堆一个塔. 最高层有1个方块,每往下一层块数+ ...

  10. 备份/还原MySQL数据库----MySQL Workbench

    点击[Data Export],界面右侧将显示数据导出窗口. 2 点击[Refresh]按钮,刷新数据库列表(1),选择要导出的数据表(2),设置导出的目录(3),点击[Start Export]按钮 ...