今天在线上出现一个UnsupportedOperationException导致接口报错:
然后定位错误位置为
然后我们查看Page.getContent的源码发现内容如下
然后我们再查看Collections中的源码,查找addAll方法
恩,知道错误了,于是将上面代码改成如下即可
List<DailyEnterprise> list = new ArrayList<>();
//........................
list.addAll(pageg.getContent()); //这里
Page<DailyEnterprise> pagel = dailyEnterpriseRepository.findAll(queryParamsl, new PageRequest(0, 7 - list.size()));
list.addAll(pagel.getContent());
其实最初list也实例化成了ArrayList,但是后面不小心直接赋值了list,而忘记了list的类型已经由ArrayList转变成了Collections,因此出现错误!