数组入参

259 阅读1分钟

数组入参有两种形式,spring后端接参也有2中形式: 如入参是[1,2,3],两种方式效果一样

  • 数组:arr=1&arr=2&arr=3
  • 字符串,英文逗号隔开:arr=1,2,3 //应该是spring框架自动转换

后端接参:两种方式效果一样

  • 数组:array(@RequestParam String[] arr)
  • list:array(@RequestParam List arr) //应该是spring框架自动转换

测试代码如下:

package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数组入参测试
 * on 2021/5/21.<br/>
 */
@RestController
@RequestMapping("/test1")
@Slf4j
public class Test1Controller {

    /**
     * 入参同等于 String[] arr
     */
    @GetMapping("arrayStr")
    public List<String> arrayStr(@RequestParam String arr) {
        List<String> list = Arrays.asList(arr);
        log.info("list:{}",list);
        return list;
    }

    /**
     * 入参同等于 String[] arr
     * @param arr
     * @return
     */
    @GetMapping("arrayList")
    public List<String> arrayList(@RequestParam List<String> arr) {
        log.info("list:{}",arr);
        return arr;
    }

    /**
     * http://localhost:8091/studydemo/test1/array?arr=1&arr=2&arr=3
     * 或 http://localhost:8091/studydemo/test1/array?arr=1,2,3,4
     * @param arr 返参可是数组,同等于List<String>
     * @return
     */
    @GetMapping("array")
    public String[] array(@RequestParam String[] arr) {
        for (String s : arr) {
            log.info("arr:{}",s);
        }
        return arr;
    }

    /**
     * http://localhost:8091/studydemo/test1/array2?arr=1,a,b&arr=2,c,d 成功
     * http://localhost:8091/studydemo/test1/array2?arr[]=1,a,b&arr[]=2,c,d 失败
     * http://localhost:8091/studydemo/test1/array2?arr[]=[1,a,b]&arr[]=[2,c,d] 失败
     * 失败的错误都是: [org.springframework.web.bind.MissingServletRequestParameterException:
     * Required request parameter 'arr' for method parameter type String[][] is not present]
     *
     * 如果是数组里面同一层级元数含义不同,建议改成json形式
     * 如[[组1,成员1,成员2],[组2,成员1,成员2]],组和成员维度不同,不应该放一起。
     *
     * @param arr
     * @return
     */
    @GetMapping("array2")
    public String[][] /*返回值同等List<List<String>>*/ array2(@RequestParam String[][] arr) {
        List<List<String>> listList = new ArrayList<>();
        for (String[] a : arr) {
            listList.add(Arrays.asList(a));
            log.info("arr a:{}", (Object) a);
            for (String s : a) {
                log.info("arr s:{}",s);
            }
        }

        log.info("listList.size:{}",listList.size());

        return arr;
    }

    /**
     * 入参同等于 String[][] arr
     * @param arr
     * @return
     */
    @GetMapping("array2List")
    public List<List<String>> array2List(@RequestParam List<List<String>> arr) {
        for (List<String> a : arr) {
            log.info("arr a:{}",a);
            for (String s : a) {
                log.info("arr s:{}",s);
            }
        }
        return arr;
    }
}