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

目录

[1. 使用数组传参](#1. 使用数组传参)

[1.2 传递单个参数](#1.2 传递单个参数)

[1.3 传递多个名称相同的参数](#1.3 传递多个名称相同的参数)

[1.3.1 关于urlencode](#1.3.1 关于urlencode)

[2. 使用集合传参](#2. 使用集合传参)


1. 使用数组传参

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

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博客https://blog.csdn.net/m0_63299495/article/details/136307074不止Chrome,postman也支持urlencode操作,在postman上构造这种类型的请求也可以成功发送并成功收到响应;

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

.java文件内容如下:

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文件内容如下:

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文件如下:

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博客https://blog.csdn.net/m0_63299495/article/details/136488702

相关推荐
从琳开始9896 小时前
从Socket到UDP回显服务器:Java网络编程完整指南
java·网络·网络协议·udp·java-ee
软件技术NINI6 小时前
基于SpringBoot和Vue的摇滚乐鉴赏网站
vue.js·spring boot·后端·spring·html·毕业设计·毕设
一枕槐安4871 天前
RAG 服务扛住高并发:缓存、队列、限流三件套
java·spring·缓存·异步任务·rag·断路器
YDS8291 天前
Small Spring IOC篇:作用域和FactoryBean
java·后端·spring
雷一言说1 天前
Spring AI 2.0 Modular RAG:接入 Rerank 重排模型
spring
学到老活到老1 天前
第 22 集:Filter、Interceptor、AOP 都能拦截,为什么不能互换?
java·后端·spring
郭小李1 天前
spring 中的 @EnableWebMvc注解和WebMvcConfigurer接口
java·后端·spring
CoderYanger1 天前
Java EE 进阶:5. Spring IoC&DI(第二弹):Spring IoC
java·开发语言·spring boot·spring·面试·职场和发展·java-ee
霸道流氓气质1 天前
AI 翻译插件:翻译记忆级联复用、术语混合检索与结构化质量门禁的 Vue3 + Spring AI 生产级实战
java·人工智能·spring
霸道流氓气质1 天前
智能表单生成器:结构化输出契约、JSON Schema 驱动渲染与 EAV/JSONB 混合存储的 Vue3 + Spring AI 生产级实战
人工智能·spring·json