Failed to instantiate [java.util.List]: Specified class is an interface
错误信息提示:
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<configs> configList。
<span style="font-family:'宋体', Arial, Helvetica, 'san-serif';">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"。
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<Sample> Map<String, Sample>等集合泛型
具体步骤
1 要配置驱动注解<mvc:annotation-driven/> ,里面注册了会把json绑定到list的"Bean实例"。
2 前台传json数组,后台直接@RequestBody List<Config> list接收就可以。
(这种方法还没测试过,只是在查这个问题的时候看到这种方式。)
Failed to instantiate [java.util.List]: Specified class is an interface的更多相关文章
- 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface
后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate ...
- Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
最近项目中页面比较复杂,springMVC传参过程中遇到这样一个错误:Could not instantiate bean class [java.util.List]: Specified clas ...
- BeanInstantiationException: Failed to instantiate [java.time.LocalDateTime]
错误提示: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationExce ...
- 严重: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component
自己写了个最简单的springMVC项目练练手,没有用maven,在WebContent中新建了lib文件夹,将jar包复制到这里面,然后add to build path到项目里. 启动Tomcat ...
- Maven使用tomcat7-maven-plugin插件run时出现错误: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component
错误如下: A child container failed during startjava.util.concurrent.ExecutionException: org.apache.catal ...
- java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]
本文为博主原创,未经允许不得转载: 被坑了好长时间的bug,差点就要重新配置环境,重新下载,重新开始的境遇.在此记录一下: 首先展示一下报错的异常: -Apr- ::] org.apache.cata ...
- java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/xiaozao_web]]
二月 20, 2017 11:30:28 上午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRul ...
- java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component...
今天开发犯了一个特lowB的错,记录下来,引以为戒! 严重: A child container failed during start java.util.concurrent.ExecutionE ...
- java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start com ...
随机推荐
- spring boot使用java读取配置文件,DateSource测试,BomCP测试,AnnotationConfigApplicationContext的DataSource注入
一.配置注解读取配置文件 (1)@PropertySource可以指定读取的配置文件,通过@Value注解获取值 实例: @PropertySource(val ...
- MAC安装远程工具Securecrt的破解方式(详细有图)
想要实现mac的远程连接功能,本来想使用终端的,但是终端的很多功能是欠佳的,所以决定安装一款,像windows的xshell一样好的软件,所以选择了这款Securecrt. 首先准备两个东西,一个是S ...
- ISO 8895-1
https://en.wikipedia.org/wiki/ISO/IEC_8859-1#Codepage_layout http://czyborra.com/charsets/
- tensorflow1.12 cuda10 cudnn7
https://download.csdn.net/download/giselite/10909984 https://blog.csdn.net/chary8088/article/details ...
- SQLite数据库下载、安装和学习
SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使用方便.结构紧凑.高效.可靠.与其他数据库管理系统不同,SQLite 的安装和运行非常 ...
- (匹配 最小路径覆盖)Air Raid --hdu --1151
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1151 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- PHP 7 安装 Memcache 和 Memcached 总结
Memcache 与 Memcached 的区别 Memcached 是 Memcache 的升级版,优化了 Memcache,并增加了一些操作方法.所以现在基本都是用最近版本的. PHP 7 下安装 ...
- [ACM_动态规划] hdu1003 Max Sum [最大连续子串和]
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- 百分之 95% 的程序员不知道 Trending 是什么。
前言如果学习到的知识不成体系,那么遇到问题时就会非常难解决.常有人问你从哪里了解新技术怎么判断其发展趋势的,除了关注 Hacker News 以及庞大的 Awesome 还有没有其它方式?有啊当然是每 ...
- ASP.NET开发常用简单实用的方法
ASP.NET开发简单实用的方法 一.打印和导出 打印和导出EXCEL在目前ASP.NET开发中可以说是必要的,有时候针对不同数据难易程度下,用有效快速的方法是解决办法的有效途径之一. 1.打印 后台 ...