SpringBoot中缓存@Cacheable出错

SpringBoot中使用@Cacheable:

错误代码:

java 复制代码
@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();   // ----------- ①
}

运行程序,出错:

java 复制代码
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] 

解决方案

只需要将编号①处的代码修改为:

java 复制代码
.collect(Collectors.toList());

原理

  • collect(Collectors.toList())‌返回的数据:
  • ‌toList()‌返回的数据:

collect(Collectors.toList()) 和 toList() 的主要区别在于返回的列表类型和可变性:

  • collect(Collectors.toList())‌:返回的是一个普通的 ArrayList ,因此可以进行添加、删除和修改操作‌
  • ‌toList()‌:返回的是通过对原始数组创建一个不可修改的列表。一旦创建,就不能对其进行添加、删除或修改操作‌

‌使用场景‌:

  • ‌toList()‌:适用于不需要对列表进行修改的场景,如从数据库查询数据等,因为它返回的是不可变列表,可以防止数据被意外修改‌
  • ‌collect(Collectors.toList())‌:适用于需要对列表进行修改的场景,因为它返回的是普通的ArrayList,可以进行各种操作‌
相关推荐
摇滚侠1 小时前
Spring Boot 3零基础教程,WEB 开发 自定义静态资源目录 笔记31
spring boot·笔记·后端·spring
摇滚侠2 小时前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 遍历 笔记40
spring boot·笔记·thymeleaf
橘子海全栈攻城狮2 小时前
【源码+文档+调试讲解】基于SpringBoot + Vue的知识产权管理系统 041
java·vue.js·人工智能·spring boot·后端·安全·spring
Json_3 小时前
学习springBoot框架-开发一个酒店管理系统,熟悉springboot框架语法~
java·spring boot·后端
kkkkk0211064 小时前
微服务学习笔记(黑马商城)
java·spring boot·spring·spring cloud·sentinel·mybatis·java-rabbitmq
冲鸭ONE4 小时前
新手搭建Spring Boot项目
spring boot·后端·程序员
数智顾问4 小时前
Flink ProcessFunction 与低层级 Join 实战手册:多流广告计费精确去重
java·spring boot·spring
Json____5 小时前
最近我用springBoot开发了一个二手交易管理系统,分享一下实现方式~
java·spring boot·后端
喜欢读源码的小白5 小时前
SpringBoot的启动流程原理——小白的魔法引擎探秘
java·开发语言·spring boot·springboot启动原理
摇滚侠6 小时前
Spring Boot 3零基础教程,WEB 开发 通过配置类代码方式修改静态资源配置 笔记32
java·spring boot·笔记