一、问题发现
项目中需要将部分资源存入redis中,数据类型为hash类型,代码使用的redisTemple。
Map map = new HashMap<String,Object>();
map.put("2021-03-12","100");
redisUtil.hashMultiSet("testHash",map);
redisUtil中的代码为
public void hashMultiSet(final String key, Map<?, ?> map) {
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.putAll(key, map);
}
运行结果为
通过redis可视化工具可以查看到,所有的key和value还有hashkey和hashvalue的原始字符前,都加了一串字符。查了一下,这是JdkSerializationRedisSerializer进行序列化时,加上去的。
二、问题解决
通过查询了解到现在可用的RedisSerializer主要有几种:
(1)StringRedisSerializer
(2)Jackson2JsonRedisSerializer
(3)JdkSerializationRedisSerializer
(4)GenericToStringSerializer
(5)OxmSerializer
本次使用StringRedisSerializer来序列化和反序列化redis的key值,使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
package com.chinaunicom.utils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* 创建自定义redisTemplate
/
@Configuration
@ConditionalOnProperty(name = "jedis", havingValue = "true")
@ConditionalOnClass(RedisOperations.class)
public class RedisAutoConfig {
private static Logger logger = LoggerFactory.getLogger(RedisAutoConfig.class);
/**
* 自定义redisTemplate,修改序列化方式
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
redisTemplate.setValueSerializer(serializer);
redisTemplate.setHashValueSerializer(serializer);
// 使用StringRedisSerializer来序列化和反序列化redis的key值
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
logger.info("Custom RedisTemplate success.");
return redisTemplate;
}
@Bean
@ConditionalOnBean(name = "redisTemplate")
public RedisUtil redisUtils(RedisTemplate redisTemplate) {
RedisUtil redisUtils = new RedisUtil();
redisUtils.setRedisTemplate(redisTemplate);
return redisUtils;
}
}
配置类加上,完美解决当前问题