Failed to instantiate [java.util.List]: Specified class is an interface

本文介绍了解决SpringMVC框架中使用列表参数时出现的错误“Failed to instantiate [java.util.List]: Specified class is an interface”的三种方法,包括重新封装VO对象、序列化为JSON字符串以及利用Spring 3.2的泛型集合支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误信息提示:

Failed to instantiate [java.util.List]: Specified class is an interface;

错误信息意思:参数错误,参数封装出了问题。

原因:

前端给后台传递了一个list对象,本来以为直接用list 可以接收,但是运行方法报错,参数错误。查询错误问题,发现是前端传递的对象,后台没有set,get的实体接收。

controller中参数List内封装的不是基本数据类型,而是一个对象,springMVC源码获取前台的参数是:request.getParameter(” “)来接收参数的,这样的话,封装参数时就出问题了。

解决办法:

第一种方法:

将VO对象再进行封装。

import java.util.List;

public class ConfigListForm {

private List<Config> ConfigList;  


public List<<span style="font-family:Arial, Helvetica, sans-serif;">Config</span>> getConfigList() {  
    return ConfigList;  
}  


public void setConfigList(List<Config> configList) {  
    ConfigList = configList;  
}  

}
  

缺陷:

这种方式不好处理接收的数据。我想接受的数据是config对象的数组,但是接收的数据是:[{configName=111, configId=111},{configName=222, =222}],不能自动封装到我的对象里,没有把configName,configId,封装到Config对象中。

第二种方法:

可以把数组序列化成Json字符串提交,后台springmvc里用@ RequestBody String 方式接收,然后把这个接收到的json串用json工具转换为数组,这样就解决了springmvc不能绑定对象数组的问题了。

将对象数组用{“list”:JSON.stringify(array)}绑定到后台,后台用@RequestBody String configs接收,接收的是json数据,然后用jackson把configs转为数组List configList。

var configList= JSON.stringify([
{configName: “sgs-demo”, configId: “1”},
{configName: “sgs-demo-1”, configId: “2”}
]);

    $.ajax({      
        type: "post",     
        url: "/config",     
        data:</span><span style="font-family:SimSun;"><span style="font-size:14px;line-height:25.2px;background-color:rgb(250,250,250);">c</span><span style="font-size:14px;line-height:25.2px;background-color:rgb(250,250,250);">onfigs</span></span><span style="font-family:'宋体', Arial, Helvetica, 'san-serif';"> ,      
        contentType: "application/json; charset=utf-8",      
        dataType: "json",      
        success: function (response, ifo) {      
            alert("success");      
        }, error: function () {      
            alert("error");      
        }      
    })   </span>  

dataType:’json’,//预期的服务器响应的数据类型。

contentType: “application/json; charset=utf-8”,//发送数据到服务器时所使用的内容类型。默认是:”application/x-www-form-urlencoded”。

如果不加contentType,configs接收的数据为类似%7B%22id%22%3A243%2C%name%22%3A4%2C%22age%22%3A1048%2C%22格式,json转换会报错,
controller层
@RequestMapping(value = “/config”, method = RequestMethod.POST)
public void myDomain(HttpServletRequest request, @RequestBody String configs) throws Exception{

    ObjectMapper objectMapper = new ObjectMapper();    
    JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, configs.class);    
    List<configs> list = objectMapper.readValue(configs, javaType);    

    System.out.println("");      
}   

  第三种方法:spring3.2 直接支持泛型集合

spring 3.2 直接支持泛型集合,如List Map

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值