springboot中配置了FastJson2JsonRedisSerializer

@Override
    public byte[] serialize(T t) throws SerializationException {
        if (t == null) {
            return new byte[0];
        }
        return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
    }

    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        String str = new String(bytes, DEFAULT_CHARSET);

        return JSON.parseObject(str, clazz);
    }

RedisTemplate会在插入和取值时用FastJson序列化和反序列化,

而我在往Redis中插值时使用了Lua脚本,插入了一个String类型,这个String类型没有用JSON.toJSONString()转换,然后在取值方法里用了默认的RedisTemplate.opsForZSet.range去取值,就出现了序列化问题。报错:

redis com.alibaba.fastjson.JSONException: syntax error, pos 1, line 1, column 2

然后在Lua脚本执行方法把member套个JSON.toJSONString()就没问题了。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐