• Redis取值时取出的是HashMap


    java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.rampage.model.BankInfo

    在改写了序列化方式之后,怎么从Redis反序列化出来的时候所有对象都变成了LinkedHashMap????

    因为我定义的MyObjectMapper没有配置DefaultTyping属性,jackson将使用简单的数据绑定具体的java类型,其中Object就会在反序列化的时候变成LinkedHashMap......再回过头来看下xml中的json序列化实现类

            objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
    

      然后我在RedisConfig类中的改写序列化方式部分加入了这个,就OK了

    @Component
    @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600)
    public class RedisConfig {
        @Bean
        public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
            RedisTemplate redisTemplate = new RedisTemplate();
            redisTemplate.setConnectionFactory(redisConnectionFactory);
    
            //解决Redis  key的序列化方式
            StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
            redisTemplate.setKeySerializer(stringRedisSerializer);
    
            //解决Redis value的序列化方式
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =new Jackson2JsonRedisSerializer(Object.class);
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
            SimpleModule simpleModule = new SimpleModule();
            simpleModule.addSerializer(DateTime.class,new JodaDateTimeJsonSerializer());
            simpleModule.addDeserializer(DateTime.class,new JodaDateTimeJsonDisSerializer());
            objectMapper.registerModule(simpleModule);
            jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
            redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
            return redisTemplate;
        }
    }
    View Code
  • 相关阅读:
    Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)D. Frequency of String
    edu30F. Forbidden Indices
    QQ登录模拟GUI综合
    Thread与Runnable线程继承与实现
    windowsProgram系统强制隐藏解除代码
    最长公共上升子序列(LCIS问题)
    分层图学习笔记 & [JLOI2011]飞行路线题解
    Luogu P1441 砝码称重
    NOIP 2020 复习计划
    浅析BST二叉搜索树
  • 原文地址:https://www.cnblogs.com/figsprite/p/11004060.html
Copyright © 2020-2023  润新知