一、 出现场景
如果前端传递的参数是数组或者列表格式时,最大的限制是[256],如果超过了这个限制就会报错
看下报错的类 DataBinder.java
public static final int DEFAULT_AUTO_GROW_COLLECTION_LIMIT = 256;
二、 解决方案
修改application.yml
配置文件
server:
max-http-header-size: 20000
增加局部配置
,在调用方法的Controller中增加【推荐】
@InitBinder //类初始化是调用的方法注解
public void initBinder(WebDataBinder binder) {
binder.setAutoGrowNestedPaths(true);
binder.setAutoGrowCollectionLimit(1024);
}
增加全局配置项
,这样添加的话,如果采用路由传参,无法直接转化为数组或者列表。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
/**
* Spring前端传递列表和数组限制大小256问题
*/
@Configuration
public class ArrayLimitConfig {
@Autowired
public ArrayLimitConfig(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
requestMappingHandlerAdapter.setWebBindingInitializer(new MyWebBindingInitializer());
}
/**
* 配置请求集合上限数量
* @author
* @version 2020-10-14
*/
public static class MyWebBindingInitializer extends ConfigurableWebBindingInitializer {
@Override
public void initBinder(WebDataBinder binder) {
super.initBinder(binder);
binder.setAutoGrowNestedPaths(true);
//配置集合上限数量
binder.setAutoGrowCollectionLimit(10000);
}
}
}