zoukankan      html  css  js  c++  java
  • SpringMvc之参数绑定注解详解之二

    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.   
    5.   @RequestMapping("/pets/{petId}")  
    6.   public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {      
    7.     // implementation omitted  
    8.   }  
    9. }  

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

     

  • 相关阅读:
    SQL语句之奇形怪状的冷门函数
    计算累计收益
    关于SQL表字段值缺失的处理办法
    虚拟机移植到另一台机器
    分分钟搞懂rank() over(partition by)的使用
    分分钟搞懂union与union all
    【转】10分钟就能学会的.NET Core配置
    【转】依赖注入的威力,.NET Core的魅力:解决MVC视图中的中文被html编码的问题
    【转】Asp.Net Core2.0获取客户IP地址,及解决发布到Ubuntu服务器获取不到正确IP解决办法
    【转】在.net Core 中像以前那样的使用HttpContext.Current
  • 原文地址:https://www.cnblogs.com/cainiao-Shun666/p/6761460.html
Copyright © 2011-2022 走看看