使用jackson2json序列化,可以让redis中get的object直接转换为需要的对象
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.time.Duration;
@Configuration
@EnableCaching //开启缓存
@AutoConfigureAfter(RedisAutoConfiguration.class) //先加载自动配置类,再加载该类
public class RedisConfig extends CachingConfigurerSupport {
/**
* JedisPoolConfig 连接池配置
*/
@Bean
public JedisPoolConfig jedisPoolConfig(RedisProperties rp) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(rp.getPool().get("maxIdle"));
poolConfig.setMinIdle(rp.getPool().get("minIdle"));
poolConfig.setMaxTotal(rp.getPool().get("maxActive"));
poolConfig.setMaxWaitMillis(rp.getPool().get("maxWait")* 1000);
return poolConfig;
}
/**
* JedisConnectionFactory 连接工厂配置
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig pool, RedisProperties rp) {
//单机配置
RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration();
standaloneConfig.setDatabase(rp.getDbIndex());
standaloneConfig.setHostName(rp.getHost());
standaloneConfig.setPort(rp.getPort());
standaloneConfig.setPassword(RedisPassword.of(rp.getPassword()));
//通过构造器来构造jedis客户端配置
JedisClientConfiguration.DefaultJedisClientConfigurationBuilder builder =
(JedisClientConfiguration.DefaultJedisClientConfigurationBuilder) JedisClientConfiguration.builder();
//设置连接超时时间
builder.connectTimeout(Duration.ofMillis(rp.getTimeout()));
//设置读超时时间
builder.readTimeout(Duration.ofMillis(rp.getTimeout()));
JedisClientConfiguration jedisClientConfig=builder.poolConfig(pool).build();
//单机配置 + 客户端配置 = jedis连接工厂
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(standaloneConfig, jedisClientConfig);
return redisConnectionFactory;
}
/**
* RedisTemplate配置
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//将redisTemplate注册到连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 设置键(key)的序列化采用StringRedisSerializer。
// redisTemplate.setKeySerializer(new StringRedisSerializer());
// redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// 设置值(value)的序列化采用FastJsonRedisSerializer。
// FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
// redisTemplate.setValueSerializer(fastJsonRedisSerializer);
// redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
// 使用Jackson2JsonRedisSerialize 替换默认序列化(默认采用的是JDK序列化)
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
redisTemplate.setKeySerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
在获取object转换为需要的对象时,报Could not read JSON: Unrecognized field “xxxx” ,则需要在该对象的多余属性或方法上添加@JsonIgnore注解
该注解的依赖为
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>