spring boot redis获取报错SerializationException: Could not read JSON: Could not resolve type id

时间:2025-02-17 20:37:15

当在A服务里存储TokenUserInfo

在B服务去取值时,会存在报错;

由于在B服务里没有TokenUserInfo,并且需要B的TokenUserInfo包路径与A服务一样

: Could not read JSON: Could not resolve type id '' as a subtype of ``: no such class found

原因:

当以对象存在redis时,是会存储类型的;redis取值是会根据这个类型做 deserializeValue

[
    "",
    {
        "userCode":"8269307847510144",
        "token":"8269307847510144_X1MNEhf8hYaGMBMRfjjfM5aQSi354afoplA7JxL0i6M",
        "C9100TokenLiveDuration":"1800",
        "keepLiveTime":1685082818804,
        "userOperateTime":1685083610706,
        "c9100TokenLiveDuration":"1800"
    }
]

#get()

public final V doInRedis(RedisConnection connection) {
			byte[] result = inRedis(rawKey(key), connection);
			return deserializeValue(result);
		}

如果就是需要在B服务获取有几种赋值:
1、把实体复制到B服务,并且路径一直

2、A服务存储时不要按照实体存储

3、使用redisTemplate 去redis存储的值不做反序列化;自己去做解析

        String value = (connection -> {

            byte[] bytes = ((StandardCharsets.UTF_8));
            if ((bytes)) {
                return null;
            }

            return new String(bytes, StandardCharsets.UTF_8);
        }, true);