2023-08-11 11:51:09 +08:00
|
|
|
|
package com.jwl.driver.server.config;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
|
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
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.RedisTemplate;
|
|
|
|
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
|
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created on 2020/6/23
|
|
|
|
|
*/
|
|
|
|
|
@Configuration
|
|
|
|
|
public class RedisConfig {
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
|
|
|
|
|
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
|
|
|
|
|
redisTemplate.setConnectionFactory(connectionFactory);
|
|
|
|
|
|
|
|
|
|
//使用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);
|
2023-09-03 22:38:58 +08:00
|
|
|
|
redisTemplate.setHashValueSerializer(serializer);
|
2023-08-11 11:51:09 +08:00
|
|
|
|
//使用StringRedisSerializer来序列化和反序列化redis的key值
|
2023-09-03 22:38:58 +08:00
|
|
|
|
redisTemplate.setKeySerializer(serializer);
|
|
|
|
|
redisTemplate.setHashValueSerializer(serializer);
|
|
|
|
|
// redisTemplate.setKeySerializer(new StringRedisSerializer());
|
2023-08-11 11:51:09 +08:00
|
|
|
|
redisTemplate.afterPropertiesSet();
|
|
|
|
|
return redisTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|