• 接口Request传参的常用注解


    1、 @PathVariable

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

    @Controller 
    @RequestMapping("/owners/{ownerId}") 
    public class RelativePathUriTemplateController { 
     
      @RequestMapping("/pets/{petId}") 
      public void findPet(@PathVariable String ownerId,@PathVariable String petId, Model model) {     
        // implementation omitted 
      } 
    } 

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

    2、 @RequestHeader和@CookieValue

      @RequestHeader 注解,可以把Request请求header部分的值绑定到方法的参数上。

      这是一个Request 的header部分:

    Host                    localhost:8080 
    Accept                  text/html,application/xhtml+xml,application/xml;q=0.9 
    Accept-Language         fr,en-gb;q=0.7,en;q=0.3 
    Accept-Encoding         gzip,deflate 
    Accept-Charset          ISO-8859-1,utf-8;q=0.7,*;q=0.7 
    Keep-Alive              300 
    @RequestMapping("/displayHeaderInfo.do")
    public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
                                  @RequestHeader("Keep-Alive") long keepAlive)  {
     
      //...

    上面的代码,把request header部分的 Accept-Encoding的值,绑定到参数encoding上了, Keep-Alive header的值绑定到参数keepAlive上。

    @CookieValue 可以把Request header中关于cookie的值绑定到方法的参数上。

    例如有如下Cookie值:

    //如JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84
    @RequestMapping("/displayHeaderInfo.do")
    public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie)  {
      //...
    }//即把JSESSIONID的值绑定到参数cookie上。

    3、@RequestParam, @RequestBody

    @RequestParam

      常用来处理简单类型的绑定,该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;

    @RequestBody

      该注解常用来处理Content-Type: application/json, application/xml等内容;

  • 相关阅读:
    .Net C# ASP.Net和ADO.Net
    如何学习.Net的步骤
    PHP5.2.17版本 fgetcsv函数 读取中文bug
    大数据入门至精通视频集
    Rethinking Table Recognition using Graph Neural Networks
    GRAPH ATTENTION NETWORKS(GAT)图注意力网络
    六个步骤快速学习难以掌握的资料
    学会总结
    数据结构学习-AVL平衡树
    数据结构学习-BST二叉查找树 : 插入、删除、中序遍历、前序遍历、后序遍历、广度遍历、绘图
  • 原文地址:https://www.cnblogs.com/lcx20190724xxz/p/11237182.html
Copyright © 2020-2023  润新知