错误信息提示:

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"。

如果不加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<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的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. BeanInstantiationException: Failed to instantiate [java.time.LocalDateTime]

    错误提示: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationExce ...

  4. 严重: 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 ...

  5. 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 ...

  6. java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]

    本文为博主原创,未经允许不得转载: 被坑了好长时间的bug,差点就要重新配置环境,重新下载,重新开始的境遇.在此记录一下: 首先展示一下报错的异常: -Apr- ::] org.apache.cata ...

  7. 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 ...

  8. java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component...

    今天开发犯了一个特lowB的错,记录下来,引以为戒! 严重: A child container failed during start java.util.concurrent.ExecutionE ...

  9. java.util.concurrent.ExecutionException

    java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start com ...

随机推荐

  1. spring 课程

    官网 参考文档 // 1. Spring_HelloWorld 20:22 // 2. Spring_IOC&DI概述 08:07 // 3. Spring_配置 Bean 21:58 // ...

  2. 2) 下载Maven

    http://maven.apache.org/ http://maven.apache.org/download.cgi Maven 3.3.3 (Binary tar.gz) Maven 3.3. ...

  3. Android:手把手教你打造可缩放移动的ImageView(上)

    定义ImageView,实现功能如下: 1.初始化时图片垂直居中显示,拉伸图片宽度至ImageView宽度. 2.使用两根手指放大缩小图片,可设置最大放大倍数,当图片小于ImageView宽度时,在手 ...

  4. mysql中设置默认字符编码为utf-8

    使用过Linux的同志就知道,在Linux下安装mysql,尤其是使用yum安装的时候,我们是没法选择其默认的字符编码方式.这个就是一个比较头痛的问题,如果Linux数据库中使用到中文的时候,乱码问题 ...

  5. [mysql] mysql如何实现更新一条记录中某个字段值的一部分呢?

    场景:在平常我们使用word文档等等office办公软件时,我们常会使用搜索->替换的功能. mysql: 在mysql 中有时候,我们也需要这样子的实现: 实现 SQL 语句: update ...

  6. [FMX]在 FMX 程序中绘制单像素宽度的直线 [FMX]在 FMX 程序中绘制单像素宽度的直线

    [FMX]在 FMX 程序中绘制单像素宽度的直线 2017-10-09 • Android.Delphi.教程 • 暂无评论 • swish •浏览 353 次 在前面的一篇文章中,我介绍了一种绘制低 ...

  7. Python学习-23.Python中的函数——isinstance

    在Python中可以使用isinstance函数来判断某个值或变量是否为某个类型. 例子: print(isinstance(1,int)) print(isinstance(1,float)) pr ...

  8. python 实现九型人格测试小程序

    用python实现九型人格测试,并把测试结果绘制成饼图,实现代码如下: # @Description: 九型人格 import xlrd, matplotlib.pyplot as plt data ...

  9. 获取微信签名,并保存在xml文件中

    using System; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using Sys ...

  10. JWT+Log4net配置与使用

    Log4net的优点        log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.程序运行过 ...