今天spring boot2.3.0整合redis进行JSON的缓存,报了这么一个错:
org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unexpected character (‘¬‘ (code 172)): expected a valid value (JSON String, Number, Array, Object or token ‘null‘, ‘true‘ or ‘false‘)org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unexpected character (‘¬‘ (code 172)): expected a valid value (JSON String, Number, Array, Object or token ‘null‘, ‘true‘ or ‘false‘)
解决:之前的序列化方式不是JSON的,但是已经缓存到Redis中没有删除,所以……
过程:
嗯……搞不懂,说我序列化失败,早来找去都不知道错哪了,鉴于RedisCacheManager是复制过来的,就去看了看源代码,也没觉得这个代码写错了。
然后搞来搞去,浪费了2个半小时,后来没办法,我又重新开始,回到上一步的直接用原本的序列化方法,不用json序列化,然后又给我报了个错:序列化的UID不匹配,嗯……刚前不久改了下UID,他这个报错我再是明白了,redis之前的缓存没有删除
回到Redis删除键,再来一次,成功。
附上代码:
@Configuration public class MyRedisConfig { @Bean public RedisCacheManager redisCacheManager (RedisConnectionFactory redisConnectionFactory){ //初始化一个redisCacheWriter RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory); //初始化序列化方式为json RedisSerializer redisSerializer = new GenericJackson2JsonRedisSerializer(); RedisSerializationContext.SerializationPair pair = RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer); RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair); return new RedisCacheManager(redisCacheWriter,redisCacheConfiguration); } }