0
点赞
收藏
分享

微信扫一扫

【JavaEE】_Spring MVC项目使用数组与集合传参

软件共享软件 03-09 21:30 阅读 2

目录

1. 使用数组传参

1.2 传递单个参数

1.3 传递多个名称相同的参数

1.3.1 关于urlencode

2. 使用集合传参


1. 使用数组传参

创建一个Spring MVC项目,其中 .java文件内容如下:

package com.example.demo.controller;

import com.example.demo.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RequestMapping("/Para")
@RestController
public class ParaController {
    @RequestMapping("/M6")
    public String m6(String[] arrayPara){
        return "ArrayPara has received:\n"
                + Arrays.toString(arrayPara);
    }
}

注:注意返回语句的书写:如果写成  return "ArrayPara has received:\n"+ arrayPara; 返回的是一个地址,需将其转为字符串进行返回;

使用浏览器构造HTTP请求发送给目标地址:

1.2 传递单个参数

1.3 传递多个名称相同的参数

即:当客户端在同一个请求中发送了多个同名的参数,浏览器会帮我们封装为一个数组

1.3.1 关于urlencode

对于1.3 中提到的在同一个请求中传递多个名称相同的参数的问题,

构造该请求时,除了使用&连接的键值对作为query string部分进行多参数传递的方法外,

还可以直接使用逗号连接多个参数值

即构造参数如下:

这种方式会被成功解析是因为Chrome浏览器会进行urlencode,

关于urlencode问题,在本专栏关于servlet项目前端向后端传参时,也曾提及,文章链接如下:

【JavaEE】_前端使用GET请求的queryString向后端传参-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/m0_63299495/article/details/136307074不止Chrome,postman也支持urlencode操作,在postman上构造这种类型的请求也可以成功发送并成功收到响应;

可以加上数组长度的输出语句,验证urlencode操作的执行:

.java文件内容如下:

package com.example.demo.controller;

import com.example.demo.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RequestMapping("/Para")
@RestController
public class ParaController {
    @RequestMapping("/M6")
    public String m6(String[] arrayPara){
        return "ArrayPara has received:\n"
                + Arrays.toString(arrayPara)
                + "\nThe length is: " + arrayPara.length;
    }
}

运行启动类后,使用postman构造如下请求并发送 :

2. 使用集合传参

以List接口为例:

.java文件内容如下:

package com.example.demo.controller;

import com.example.demo.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RequestMapping("/Para")
@RestController
public class ParaController {
    @RequestMapping("/M7")
    public String m7(List<String> listParam){
        return "ListParam has received:\n" +listParam
                +"\nThe length is: " + listParam.size();
    }
}

运行启动类后,使用postman构造如下请求并发送 :

查看错误日志:

表示:默认封装方式为数组,而非List接口

如果需要使用List接口,需要使用一个注解@RequestParam

现修改.java文件如下:

package com.example.demo.controller;

import com.example.demo.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RequestMapping("/Para")
@RestController
public class ParaController {
    @RequestMapping("/M7")
    public String m7(@RequestParam(required = false) List<String> listParam){
        return "ListParam has received:\n" +listParam
                +"\nThe length is: " + listParam.size();
    }
}

再次运行启动类,使用postman构造如下请求并发送 :

可见此时参数传递成功;

注:关于@RequestParam注解,在后端代码重命名时也曾使用过,此部分相关原文链接如下:

【JavaEE】_Spring MVC项目之使用对象传参-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/m0_63299495/article/details/136488702

举报

相关推荐

0 条评论