[Spring Error] Invalid list index 256

Background

Using List<Integer> ids to receive a form parameter of HTTP request. The size of the parameter ids is over 255.

Error Info

org.springframework.beans.InvalidPropertyException: Invalid property 'ids[256]' of bean class [com.fantasi.manage.console.web.modules.recovery.dto.RecoveryGroupDto]: Invalid list index in property path 'ids[256]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256. 

Solutions

To override init Binder method and configure the limit size which needs so that you can pass those many objects to your controller classes.

@Controller
public class MyController {

@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setAutoGrowCollectionLimit(500);
}

...
}

@InitBinder plays the role to identify the methods which initialize the WebDataBinder.

WebDataBinder is a DataBinder that binds request parameter to JavaBean objects.

Reasons

By default spring only maps only 255 objects to the java bean. Spring developers has done this to prevent OutOfMemory problem.

References

[1] Spring initBinder for Handling Large List of Java Objects