账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    SpringBoot中使用@Cacheable注解让Redis缓存数据问题
    35
    0

    我加上@Cacheable注解想让Redis缓存我从数据库取得的数据,但是报了以下错误。猜测是RedisTemplate配置中无法将jsonarray转换的问题,但我无法解决,希望各位能帮助我一下。
    当我去掉@Cacheable注解时可正常返回json数据,数据类似:

    {
        "result": true,
        "msg": "ok",
        "exp": [  <--猜测是这个jsonarray所导致的问题
            {
                "id": 1,
                "nickname": "管理员",
                "userName": "admin@test.com",
                "password": "4e4d8243d98598979d7bbfd75882a326cb669bae8dd4440e7cec58a4221d5535",
                "salt": "fDmxl2ggQ98hi8WdbDtKxK60LPRFTISS",
                "status": 1,
                "createAt": 1524013219000,
                "ip": "xxx.xxx.xxx.xxx",
                "avatar": "0/1.jpg",
                "likeCount": 999
            }
        ]
    }

    报错信息:

    org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Can not deserialize instance of com.kellen5l.experimentalplatform.entity.AccountEntity out of START_ARRAY token
     at [Source: [B@26191388; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.kellen5l.experimentalplatform.entity.AccountEntity out of START_ARRAY token

    RedisTemplate:

        @Bean
        public RedisTemplate<String, Object> accountRedisTemplate(
                RedisConnectionFactory redisConnectionFactory)
                throws UnknownHostException {
            RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
            template.setConnectionFactory(redisConnectionFactory);
            Jackson2JsonRedisSerializer<AccountEntity> jsonRedisSerializer = new Jackson2JsonRedisSerializer<AccountEntity>(AccountEntity.class);
            template.setDefaultSerializer(jsonRedisSerializer);
            return template;
        }
        @Bean
        public RedisCacheManager accountCacheManager(RedisTemplate<String, Object> accountRedisTemplate) {
            RedisCacheManager cacheManager = new RedisCacheManager(accountRedisTemplate);
            cacheManager.setUsePrefix(true);
            return cacheManager;
        }
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部