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中的名称。

最新文章

  1. devpress 的gridview 控件的行拖拽 z
  2. C++IO关于cin>>和getline的理解
  3. net-snmp的MIBs扩展(linux下)
  4. Delphi中Format与FormatDateTime函数详解
  5. javaEE(web)SEO优化 Yahoo军规
  6. bzoj4130: [PA2011]Kangaroos
  7. C++设计模式-AbstractFactory抽象工厂模式
  8. js object(对象)
  9. 07 - 删除vtkDataObject中的SetExtentTranslator 和 GetExtentTranslator方法 VTK 6.0 迁移
  10. Audio笔记之MediaPlayerService:setDataSource
  11. THUSC2015
  12. mysql sql语句大全(转载)
  13. Linux: curl
  14. “史上更难就业季”暴露出啥隐情?
  15. None.js 第三步 回调函数【阻塞代码--非阻塞代码】
  16. jquery利用正则表达式验证密码,手机号(主要是使用方法,正则表达式网上一搜一堆)
  17. Centos7 -- glibc 升级失败、意外删除、故意删除后的处理方法
  18. Android图片处理(Matrix,ColorMatrix)
  19. opencv-3.3安装记录-ubuntu 14.04
  20. vc6中向vs2010迁移的几个问题

热门文章

  1. windows下php升级到7.2
  2. 创建美国地区的appleId
  3. LeetCode:颜色分类【75】
  4. html5 css3 进度条特效
  5. oss2模块和aliyun oss链接
  6. 在我的电脑右键 Manage 拒绝访问的解决方法
  7. EntityFramework 学习 一 Local Data
  8. Storm- Storm作业提交运行流程
  9. Hive- 大数据仓库Hive
  10. 通过request获取ID地址的方法