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()就没问题了。
本文探讨了在SpringBoot应用中,使用FastJson2JsonRedisSerializer进行Redis数据序列化时遇到的问题。当直接插入非序列化的String类型到Redis并尝试通过RedisTemplate获取时,会引发JSON解析错误。解决方案是在Lua脚本中确保数据经过JSON.toJSONString()转换,以保证正确序列化。此问题突显了在微服务架构中数据序列化一致性的重要性。
22万+

被折叠的 条评论
为什么被折叠?



