• Get请求如何传递数组、对象


    Get请求如何传递数组

    1、将数组参数传递多次

    可以将数组参数传递多次,springmvc会将多个同名参数自动封装成数组或者集合对象,示例如下:

    请求URL
    http://127.0.0.1:8099/springbootIntegration/test/testQuest?page=1&size=2&ids=11&ids=22

    在这里插入图片描述

    后端接口

    
    @RestController
    @RequestMapping("/test")
    public class ControllerTest {
    
        @GetMapping("/testQuest")
        public String testQuest( @RequestParam int page, @RequestParam int size, @RequestParam String [] ids){
    
            return "Hello World";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2、直接将数组指用逗号分隔

    示例如下:
    请求URL
    http://127.0.0.1:8099/springbootIntegration/test/testQuest?page=1&size=2&ids=11,22

    在这里插入图片描述
    后端接口

    
    @RestController
    @RequestMapping("/test")
    public class ControllerTest {
    
        @GetMapping("/testQuest")
        public String testQuest( @RequestParam int page, @RequestParam int size, @RequestParam String [] ids){
    
            return "Hello World";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Get请求如何传递对象

    Get请求一般用请求头来传递简单参数、但也可用Body传递对象,甚至可以一起使用。

    如下:
    Params加入page、size参数
    在这里插入图片描述
    Body中加入数组对象
    在这里插入图片描述

    后端接口

    
    @RestController
    @RequestMapping("/test")
    public class ControllerTest {
    
        @GetMapping("/testQuest")
        public String testQuest( @RequestParam int page, @RequestParam int size,  String [] ids){
    
            return "Hello World";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    我们知道@RequestParam可以通过value属性指定参数名,requ设置参数是否必须、设置参数默认值等。
    三个参数加不加@RequestParam都正确,下面两种也正确:

    @GetMapping("/testQuest")
        public String testQuest(  int page,  int size,  String [] ids){
            return "Hello World";
        }
    
    • 1
    • 2
    • 3
    • 4
     @GetMapping("/testQuest")
        public String testQuest( @RequestParam int page, @RequestParam int size,  @RequestParam String [] ids){
    
            return "Hello World";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    注意:
    接口参数String [] 加@RequestParam时,此参数只能放在GET请求的Params中
    接口参数String [] 加@RequestBody时,此参数只能放在GET请求的Body中

    若接口参数是一个List< Object> 或者实体对象 需要@RequestBody注解,参数只能放在GET请求的Body中

  • 相关阅读:
    基于映射序列码的自适应查询树防碰撞算法
    C和指针 第13章 高级指针话题 13.4 命令行参数
    Jenkins-CentOS安装jenkins
    UE4_HttpLibraryPlugins使用调用
    商业智能BI,助力企业数据文化建设
    快速了解什么是跳跃表(skip list)
    嵌入式Linux入门-手把手教你初始化SDRAM(附代码)
    暂停Windows更新方法
    机器人种类知多少
    centos7云服务器安装nginx记录
  • 原文地址:https://blog.csdn.net/qq_40419080/article/details/131138004