Spring mvc为何不能直接传递集合参数

1、@RequestMapping 里的path和value启的作用是一样的,因为value是@interface不加属性说明@RequestMapping(“”)这样默认的赋值的字段,所有@RequestMapping会有个value,path与value不能同时存在

2、spring 方法的入参是不能为接口的,比如说不能直接传递一个List<>进来

@RequestMapping("test2")
    @ResponseBody
    public Object test2(List<String> params){
        return params;
    }

像上面这种方法就不可以,因为spring在给入参赋值的时候会经过这个方法

public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException {
        Assert.notNull(clazz, "Class must not be null");
        if(clazz.isInterface()) {
            throw new BeanInstantiationException(clazz, "Specified class is an interface");
        } else {
            try {
                return instantiateClass(clazz.getDeclaredConstructor(new Class[0]), new Object[0]);
            } catch (NoSuchMethodException var2) {
                throw new BeanInstantiationException(clazz, "No default constructor found", var2);
            }
        }
    }

这里List作为一个接口是会被抛出异常的,所以spring的入参不能含有接口入参,但是做为一个入参对象里含有List却是可以的

 @RequestMapping("test3")
    @ResponseBody
    public Object test3(FrontTestModel testModel){
        return testModel;
    }
public class FrontTestModel {
    public String name;

    public Integer id;

    public List<String> phones;
}

实验结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值