Cacheable List 接口二次读取报错

java 复制代码
/**
     * 获取所有活跃的自定义字段(缓存 DTO)
     * 缓存 DTO 而非实体类,避免序列化关联对象(Space、CustomChoice)
     */
    @Cacheable(cacheResolver = "tenantDynamicCacheResolver",
            cacheNames = {"system:customField:"},
            key = "'customField'")
    public List<CustomFieldCacheDto> getAllActiveCustomFieldListInCache() {
        List<CustomField> entities = customFieldRepository.findAllDataAsEntity();
        return entities.stream()
                .map(CustomFieldConvert.INSTANCE::convertToCacheDto)        
                 .toList();

    }

二次读取会报错,

java 复制代码
     List<CustomFieldCacheDto> fieldList = service.getAllActiveCustomFieldListInCache();

将 toList() 替换为.collect(Collectors.toList()); 正常。

根本原因

1. 返回的 List 实现类不同

java

复制代码
// toList() - Java 16+ 引入
.stream().toList()  
// 返回: ImmutableCollections.ListN (不可变列表)

// Collectors.toList()
.stream().collect(Collectors.toList())
// 返回: ArrayList (可变列表)

2. 序列化器无法处理不可变列表

Redisson/Jackson 在反序列化时:

  • ArrayList:有默认无参构造器,可以正常实例化

  • ImmutableCollections.ListN:没有公共构造器,无法通过反射创建实例

相关推荐
共享家95271 天前
408 操作系统第一章
服务器·数据库·windows
Sakura_j_h_1 天前
GitHub Actions 打包 Windows exe:中文编码与产物上传
windows·ci/cd·打包发布·github·pyinstaller·github actions
xixiaoyunya1 天前
搭一个浏览器即用的二进制文件分发点:目录规范与权限边界实践
网络·windows
麻瓜code1 天前
Spring AI 全模块 API 字典一览(1.1.0 版)
人工智能·windows·spring
zrc0070071 天前
steam无法登录 steam错误 登录时出现错误。请稍后再试。解决办法 steam无法登录解决办法 湖南地区steam DNS问题解决办法
windows·经验分享·游戏·缓存·游戏程序·创业创新·玩游戏
BK贝壳1 天前
布尔队列 BHA_Queue:入队出队均摊 O(1);附 BHA_Dict
windows
-星空下无敌1 天前
Windows Docker K8s 安装部署Naocs ,SpringBoot 3.x 4.x 整合Naocs 版本兼容 配置中心、服务注册与发现
windows·docker·nacos·kubernetes·springboot·alibaba
燃烧的枫1 天前
Windows 下 YOLO11 Pose 的 TensorRT C++ 部署
windows·yolo·tensorrt·1024程序员节·yolo11-pose
深圳恒讯2 天前
香港云主机硬盘满了怎么办?Linux和Windows在线扩容操作教程
linux·运维·windows
打工仔折腾 AI2 天前
鸿蒙移动端直连 Windows 远程桌面配置与性能调优实践
人工智能·windows·后端·python·华为·性能优化·harmonyos