Ajax的post请求参数过大异常处理

Ajax的post请求参数过大异常解决

问题描述

使用Ajax的post请求时,在param中传递参数,但是参数较大,后台controller无法获得到list对象;导致报错,重点信息为:java.lang.IndexOutOfBoundsException: Index: 256, Size: 256;具体报错内容如下:


2019-02-28 17:12:37.291  INFO 22404 --- [nio-8081-exec-1] c.c.a.c.i.ErrorPageInterceptor           : 响应状态:500
2019-02-28 17:12:37.291  INFO 22404 --- [nio-8081-exec-1] c.c.a.c.i.ErrorPageInterceptor           : 请求路径:/
2019-02-28 17:12:46.453 ERROR 22404 --- [nio-8081-exec-3] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'schdDateList[256]' of bean class [com.ccx.axis.entity.structured.model.TrancheBean]: Index of out of bounds in property path 'schdDateList[256]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256] with root cause

java.lang.IndexOutOfBoundsException: Index: 256, Size: 256
	at java.util.ArrayList.rangeCheck(ArrayList.java:653) ~[?:1.8.0_91]
	at java.util.ArrayList.get(ArrayList.java:429) ~[?:1.8.0_91]
	at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:664) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.AbstractNestablePropertyAccessor.getNestedPropertyAccessor(AbstractNestablePropertyAccessor.java:848) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyAccessorForPropertyPath(AbstractNestablePropertyAccessor.java:825) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:268) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:860) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.validation.DataBinder.doBind(DataBinder.java:756) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:192) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:106) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.bindRequestParameters(ServletModelAttributeMethodProcessor.java:152) ~[spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:111) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	

报错原因

spring在接收前台传递的list时,对对象进行映射时,会使用DataBinder对象进行封装,且该对象默认封装list中兑现最多为256个,如下:
在这里插入图片描述
因此list中最多能传递256个对象,此时我传递的对象数量已经超过了256个,报了数组越界异常;此处需要做的是修改spring的dataBlinder接收list中对象的个数即可;

如何修改

方案一

在当前controller中添加一个初始化方法,在加载该controller时,首先会加载这个初始化方法,我们在这个初始化方法中设置这个controller能够接受的list中对象的个数即可;

	@InitBinder
    public void initBinder(WebDataBinder binder) { //给这个controller配置接收list的长度100000,仅在这个controller有效
         binder.setAutoGrowCollectionLimit(100000);
    }

WebDataBinder 继承DataBinder对象;并将集合中最大值设置为10万即可;

至此,该问题就可解决;

方案二

如果你的项目中有多个类似post请求,则使用上述方法显得有点麻烦,此处有一个简单的方法可以进行全局设置:
定义一个初始化类:

 public class myInitializer implements WebBindingInitializer { 
 	@Override 
	 public void initBinder(WebDataBinder binder) {
	 	binder.setAutoGrowCollectionLimit(100000);
	 	 }
	  } 

然后在配置文件配置 :

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
   <property name="webBindingInitializer">  
       <bean class="xxx.myInitializer"/>  //class指myInitializer类
   </property>  
</bean>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值