欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > 接口报错500InvalidPropertyException: Invalid property ‘xxx[256]‘,@InitBinder的使用

接口报错500InvalidPropertyException: Invalid property ‘xxx[256]‘,@InitBinder的使用

2024/10/24 12:33:22 来源:https://blog.csdn.net/qq_23888451/article/details/142495273  浏览:    关键词:接口报错500InvalidPropertyException: Invalid property ‘xxx[256]‘,@InitBinder的使用

org.springframework.beans.InvalidPropertyException: Invalid property ‘xxx[256]’ of bean class [com.xxl.MailHead]: Invalid list index in property path ‘xxx[256]’; nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256

从报错可以看到,前端传入参数个数大于256个,后端使用List接参,超出数组大小限制。
有两种方式修改:

1.局部设置

在controller中增加以下代码,只对该controller生效

@InitBinder
public void initBinder(WebDataBinder binder) {binder.setAutoGrowNestedPaths(true);binder.setAutoGrowCollectionLimit(1024);
}

这个更改做了以下几点:
增加了@InitBinder注解的方法,这将应用于这个控制器中的所有请求处理方法。
setAutoGrowNestedPaths(true)允许嵌套路径自动增长,这对于复杂对象很有用。
setAutoGrowCollectionLimit(1024)将集合的自动增长限制设置为1024,这应该足以处理您之前遇到的256个参数的限制问题。

2.全局设置

通过实现WebBindingInitializer接口并在Spring MVC配置文件中注册,可以全局设置数据绑定器的属性。适用于需要在整个应用程序中进行统一配置的情况

public class DataBindingInitializer implements WebBindingInitializer{@Override  public void initBinder(WebDataBinder binder, WebRequest request) {  binder.setAutoGrowCollectionLimit(100000);  }  
}

在xml中配置

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="webBindingInitializer"><bean class="xxx.DataBindingInitializer"/></property>
</bean>

看到其他文章说:当使用了<mvc:annotation-driven />的时候,会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean。这时候第二种方式指定的全局属性编辑器就不会起作用了,解决办法就是手动的添加上述bean,并把它们加在mvc:annotation-driven/的前面。如果不生效,则将手动注册AnnotationMethodHandlerAdapter改为手动注册RequestMappingHandlerAdapter。

也有小伙伴说第二种方式最好不使用,覆盖掉框架WebBindingInitializer,导致校验框架失效等问题

总结还是第一种局部设置比较保险,就是每个controller都需要设置

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com