您的位置:首页 > 数据库 > Redis

SpringBoot 2.3.5 整合redis缓存自定义JSON序列化中遇到的问题。

2020-06-25 00:54 645 查看

目前正在学习尚硅谷雷丰阳老师的springboot的课程。
老师课程中使用的springboot的版本是1.5,而我使用的是2.3.5。
springboot的2.x版本与1.x版本的差别还蛮大的,很多底层代码都改变了,而且变化还蛮大了。
当前我正在学习利用redis缓存自定义JSON序列化,遇到了2.x底层源码的配置自定义JSON序列元与1.x(雷老师课程中讲授的)有很大的不同的问题。
花费大量的时间百度后,尝试了n种方法后,终于找到了一种可行的方法,特此记录,以便其他遇到此问题的朋友以及自己今后的复用。

我参考的这位大神的文章:https://www.geek-share.com/detail/2802219775.html
其中最关键的是配置redis的配置类来自定义JSON序列化,代码如下:我把我的导的包也写进来了。

package com.atguigu.cache.config;

import com.atguigu.cache.bean.Employee;

import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.net.UnknownHostException;
import java.time.Duration;

@Configuration
public class MyRedisConfig {

@Bean
public RedisTemplate<Object, Employee> EmpRedisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<Object, Employee> template = new RedisTemplate<Object, Employee>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
template.setDefaultSerializer(serializer);
return template;
}

//CacheManagerCustomizers可以来定制缓存的一些规则
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory){
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(1))
.disableCachingNullValues()
.serializeKeysWith(RedisSerializationContext.SerializationPair
.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();}
}

这个方法是亲测可行的,但是也有一个小坑,我卡在这了快半个钟。
如果之前 没有配置自定义的CacheManager,就会使用默认的配置,利用Jdk来序列化,存入redis中。
在之前操作过的情况下,redis中就会保留此次记录,当再次运行后,就会报错,报错信息如下:

Failed to complete request: 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')

这个报错的原因就是因为之前在redis中以当前使用的key 缓存了一个以默认jdk来序列化的值,所以导致此次缓存的失败。
只要到redis中把之前用这个key来缓存的默认jdk序列化给删除了,就可以了。
现在再回头来看,如此简单的几步操作,前前后后竟卡了我一个多小时。
故决定写下此文,毕竟好记性不如烂笔头,也方便其他正在学习springboot的朋友们!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: