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

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

我参考的这位大神的文章:https://blog.csdn.net/weixin_44757206/article/details/106407916
其中最关键的是配置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的朋友们!

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值