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. 初步jmeter安装与使用

    前言,最近公司做了面向全国用户的教育平台,由于测试人员以功能测试为主,于是接口代码压测就被开发揽了,这就开始倒腾jmeter了,其实我想对于java,我更愿意用Python的工具,毕竟我爬虫时用的Py ...

  2. PHP网站在Linux服务器上面的安全配置

    本文详细总结了PHP网站在Linux服务器上面的安全配置,包含PHP安全.mysql数据库安全.web服务器安全.木马查杀和防范等,很好很强大很安全. PHP安全配置 1. 确保运行php的用户为一般 ...

  3. 搜索ABAP程序代码中的字符串

    标准程序名:RPR_ABAP_SOURCE_SCAN /BEV1/NERM07DOCS

  4. QT设置QToolBar带有图标和文字

    ui->mainToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

  5. HDU - 1176 免费馅饼 【DP】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1176 思路 因为刚开始的起点是固定的 但是终点不是固定的 所以我们可以从终点往起点推 dp[i][j] ...

  6. iOS NSDateFormatter 不安全线程 处理

    记得 上次我们开CodeReView大会 有人提出 "  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];   ...

  7. 第二十三篇、IO多路复用 二

    基于IO多路复用实现socket的连接 下面流程:1.先创建列表,让自己的socket对象传入,然后遍历select第一个参数2.客户端连接,那么客户端socket就传入到了列表里面,3.遍历,接收客 ...

  8. 第二篇、HTML

    一.html文档树 二.HTML分类 块级标签和内联标签: 块级标签:<p><h1><table><ol><ul><form>& ...

  9. tomcat7 中的坑。 关于welcome-list和servlet-mapping

    web.xml中, 使用default servlet设置了针对静态资源后缀名的过滤. 并且设置了welcome-list, 使用jetty和tomcat6启动一切正常, 但是使用tomcat7则出现 ...

  10. 大话设计模式--享元模式 Flyweight -- C++实现实例

    1. 享元模式: 运用共享技术有效地支持大量细粒度的对象. 享元模式可以避免大量非常相似类的开销,在程序设计中,有时需要生成大量颗粒度的类实例来表示数据,如果能发现这些实例除了几个参数外基本都是相同的 ...