SPRING接收前台传入List时,超过256报IndexOutOfBoundsException异常
2015-08-10 16:34
501 查看
首先看看异常
从前台传入detailList256个时不会报错,接收第257个时就会出现异常。。
然后看一下报异常的源代码,类名BeanWrapperImpl
根据上面的分析找到autoGrowCollectionLimit的定义
解决方案,是在自己的Controller中加入如下方法
以上。
org.springframework.beans.InvalidPropertyException: Invalid property 'detail[256]' of bean class [com.suning.asvp.mer.entity.InviteCooperationInfo]: Index of out of bounds in property path 'detail[256]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256 at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:833) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE] at org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:576) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE] at org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:553) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE] at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:914) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE] at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE] at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:692) ~[spring-context-3.1.2.RELEASE.jar:3.1.2.RELEASE] at org.springframework.validation.DataBinder.doBind(DataBinder.java:588) ~[spring-context-3.1.2.RELEASE.jar:3.1.2.RELEASE] at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:191) ~[spring-web-3.1.2.RELEASE.jar:3.1.2.RELEASE] at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:112) ~[spring-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
从前台传入detailList256个时不会报错,接收第257个时就会出现异常。。
然后看一下报异常的源代码,类名BeanWrapperImpl
else if (value instanceof List) { int index = Integer.parseInt(key); List list = (List) value; growCollectionIfNecessary(list, index, indexedPropertyName, pd, i + 1); value = list.get(index);// 测试报错时,此处list只有256个,index为256时,取第257个报错 }
@SuppressWarnings("unchecked") private void growCollectionIfNecessary( Collection collection, int index, String name, PropertyDescriptor pd, int nestingLevel) { if (!this.autoGrowNestedPaths) { return; } int size = collection.size(); // 当个数小于autoGrowCollectionLimit这个值时才会向list中添加新元素 if (index >= size && index < this.autoGrowCollectionLimit) { Class elementType = GenericCollectionTypeResolver.getCollectionReturnType(pd.getReadMethod(), nestingLevel); if (elementType != null) { for (int i = collection.size(); i < index + 1; i++) { collection.add(newValue(elementType, name)); } } } }
根据上面的分析找到autoGrowCollectionLimit的定义
public class DataBinder implements PropertyEditorRegistry, TypeConverter { /** Default object name used for binding: "target" */ public static final String DEFAULT_OBJECT_NAME = "target"; /** Default limit for array and collection growing: 256 */ public static final int DEFAULT_AUTO_GROW_COLLECTION_LIMIT = 256; private int autoGrowCollectionLimit = DEFAULT_AUTO_GROW_COLLECTION_LIMIT;
解决方案,是在自己的Controller中加入如下方法
@InitBinder protected void initBinder(WebDataBinder binder) { binder.setAutoGrowNestedPaths(true); binder.setAutoGrowCollectionLimit(1024); }
以上。
相关文章推荐
- Java的String类中常用方法举例
- JavaSE 文件递归之删除&获取文件夹目录中所有的以.jpg的文件的绝对路径
- 最大子矩阵问题实例解析
- java生成xml(详细版)
- java方法的重载
- eclipse常用插件安装
- JSON-JSON字符串转换成JSON对象、JSON对象数组、java实体类以及保存到List列表中
- eclipse搭建servlet项目
- 一个广为人知但鲜有人用的技巧:对象池
- java容器类---TreeMap、TreeSet
- myeclipse 同名工程部署问题
- spring MVC原理
- java环境变量的配置
- myeclipse安装SVN插件
- java的变量和方法,实例化new
- Java:JFrame与Frame的区别
- springmvc学习第二天
- Java中==和equals的区别
- struts2的工作流程
- Java Swing 第01记 Hello Word