redis 存取报错

redis  存非string报java.lang.ClassCastException

使得存不进去

修改配置文件添加 支持序列化代码

package com.lenovo.pcsd.bp.businesspartner.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

/**
 * Created by freezhan on 16/9/5.
 */
@Configuration
public class CacheConfig {
    //过期时间
    @Value("${redisDefaultExpiration}")
    private long redisDefaultExpiration;


    //缓存key前缀
    @Value("${spring.application.name}")
    private String cachePrefix;


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Bean
    public CacheManager cacheManager() {
        RedisCacheManager manage = new RedisCacheManager(redisTemplate);
        manage.setDefaultExpiration(redisDefaultExpiration);
        manage.setCachePrefix(new RedisPrefix(cachePrefix));
        //一下代码为新增
         redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        redisTemplate.setKeySerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        redisTemplate.setHashKeySerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        manage.setUsePrefix(true);
        return  manage;
    }

}

redis  存非string报

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to

有资料说 热部署依赖包冲突   删除了还是没有解决


      
			org.springframework.boot
			spring-boot-devtools
			true
		

最终解决方案

    @Bean
    public CacheManager cacheManager() {
        RedisCacheManager manage = new RedisCacheManager(redisTemplate);
        manage.setDefaultExpiration(redisDefaultExpiration);
        manage.setCachePrefix(new RedisPrefix(cachePrefix));
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setKeySerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
        manage.setUsePrefix(true);
        return  manage;
    }

}
JacksonJsonRedisSerializer和GenericJackson2JsonRedisSerializer,两者都能系列化成json,但是后者会在json中加入@class属性,类的全路径包名,方便反系列化。前者如果存放了List则在反系列化的时候如果没指定TypeReference则会报错java.util.LinkedHashMap cannot be cast to


{
  "@class": "com.vo.ResultVO",
  "code": 0,
  "msg": "成功",
  "data": {
    "@class": "com.utils.PageUtil",
    "number": 5,
    "size": 2,
    "numberOfElements": 2,
    "totalElements": 597,
    "totalPages": 299,
    "content": [
      "com.github.pagehelper.Page",
      [
        {
          "@class": "java.util.HashMap",
          "productgroupid": "B3",
          "shoptypename": "钻石级",
          "season3": 0,
          "citydis": "3U",
          "season4": 0,
          "id": 66,
          "season1": 0,
          "season2": 0
        },
        {
          "@class": "java.util.HashMap",
          "productgroupid": "B3",
          "shoptypename": "全星级",
          "season3": 0,
          "citydis": "3U",
          "season4": 0,
          "id": 67,
          "season1": 0,
          "season2": 0
        }
      ]
    ]
  }
}
可以看到保存到redis数据库中的数据是json,并且每个节点都有@class属性,这些属性在凡系列化时会使用到


你可能感兴趣的:(redis)