错误代码:
@cacheable(value = "frontadvertisevolist", keygenerator = "cachekey")
@override
public list<frontadvertisevo> getfrontadvertisevolist(integer count) {
return this.list(wrappers.<advertise>lambdaquery()
.select(advertise::getpic, advertise::geturl)
.eq(advertise::getstate, 1)
.orderbydesc(advertise::getpriority)
.last("limit " + count))
.stream()
.map(advertise -> new frontadvertisevo(advertise.getpic(), advertise.geturl()))
.tolist(); // ----------- ①
}
运行程序,出错:
org.springframework.data.redis.serializer.serializationexception: could not read json:unexpected token (start_object), expected value_string: need string, number of boolean value that contains type id (for subtype of java.lang.object) at [source: redacted (`streamreadfeature.include_source_in_location` disabled); line: 1, column: 2]
解决方案
只需要将编号①处的代码修改为:
.collect(collectors.tolist());
原理
collect(collectors.tolist())返回的数据:

tolist()返回的数据:

collect(collectors.tolist()) 和 tolist() 的主要区别在于返回的列表类型和可变性:
- collect(collectors.tolist()):返回的是一个普通的 arraylist ,因此可以进行添加、删除和修改操作
- tolist():返回的是通过对原始数组创建一个不可修改的列表。一旦创建,就不能对其进行添加、删除或修改操作
使用场景:
- tolist():适用于不需要对列表进行修改的场景,如从数据库查询数据等,因为它返回的是不可变列表,可以防止数据被意外修改
- collect(collectors.tolist()):适用于需要对列表进行修改的场景,因为它返回的是普通的arraylist,可以进行各种操作
到此这篇关于springboot中缓存@cacheable出错的问题解决的文章就介绍到这了,更多相关springboot缓存@cacheable出错内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论