【Java基础】以数组集合形式接收前端传递的多个参数

一、需求描述

利用postman模拟前端发送我童年喜欢的几部动画片(比如下图我最喜欢的《虹猫蓝兔七侠传》)的名称,后端分别以数组和集合的形式接收数据。

二、代码展示

在RequestController类中编辑以下代码

package com.example.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 *测试请求参数接收
 */
@RestController
public class RequestController {
      @RequestMapping("/simpleParam")
      //数组方式
//    public String simpleParam(String[] cartoon){
//        System.out.println(Arrays.toString(cartoon));
//        return "OK";
//    }

    //集合方式
    public String simpleParam(@RequestParam List<String> cartoon) {
        System.out.println(cartoon);
        return "OK";
    }
}

三、运行情况

在postman模拟前端发送数据,发送成功将接收到“OK”:

 后端收到前端发送的数据后,控制台显示动画片名字:

 四、参考教程

1、请求下响应https://www.bilibili.com/video/BV1m84y1w7Tb?p=70&vd_source=841fee104972680a6cac4dbdbf144b50

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林月明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值