错误信息提示:

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. Python初学者的捷径[译]

    下面列出的都是这些年总结的Python的有用的知识点和一些工具.希望对你有所帮助! 交换变量值 x = 6 y = 5 x, y = y, x print x >>> 5 print ...

  2. LeetCode147:Insertion Sort List

    题目: Sort a linked list using insertion sort. 解题思路: 按题目要求,直接进行插入排序 实现代码: #include <iostream> us ...

  3. NET 下载共享文件

    执行 public static void Run() { "); if (state) { // 共享文件夹的目录 TransportRemoteToLocal(@"\\192. ...

  4. Windows下安装NTP服务器

    NTP服务器介绍 NTP服务器[Network Time Protocol(NTP)]是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高 ...

  5. Effective Java(1)-创建和销毁对象

    Effective Java(1)-创建和销毁对象

  6. 用ndp部署storm应用

    本文由作者余宝虹授权网易云社区发布. 使用户ndp部署一个Java应用大家都非常熟悉的,但是看到某些同学用非常繁琐的方式部署storm应用的时候,我觉得很有必要整一个帮助教程,ndp帮助文档里面没有, ...

  7. Android 的开源项目的网址

    各种各样的Android实例  https://blog.csdn.net/qq153843338/article/details/43161669 Android 教程 http://www.run ...

  8. C++命令行画心形<转载>

    #include <stdio.h> int main() { for (float y = 1.5f; y > -1.5f; y -= 0.1f) { for (float x = ...

  9. position:absolute;宽度自适应

    http://blog.csdn.net/isaisai/article/details/45640515 设置left:0,right:0

  10. Sublime Text 格式化JSON-pretty json

    1.安装install package 按control + `,打开命令输入框 输入一下命令: import urllib2,os; pf='Package Control.sublime-pack ...