依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.5.14</version>
</dependency>
序列化配置類
package com.syh.pdd.configuration; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; 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.RedisSerializer; @Configuration public class RedisConfig { //自定義序列化,反序列化物件
public RedisSerializer jsonSerializer(){ Jackson2JsonRedisSerializer serializer =new Jackson2JsonRedisSerializer(Object.class); //基於此物件進行序列化的方式 ObjectMapper objectMapper = new ObjectMapper();
//表示get方法使用此序列化 //表示全部許可權的方法可以被序列化,預設public objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.ANY);
//修改序列化儲存型別,對想序列化會將物件的型別儲存到redis資料庫
//假如沒有這個配置,redis儲存時不儲存型別,反序列化預設儲存到map
objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(),
ObjectMapper.DefaultTyping.NON_FINAL,//啟用序列化型別儲存
JsonTypeInfo.As.PROPERTY);//表示型別會以json物件屬性形 式儲存//物件屬性為空時,不進行序列化儲存 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); serializer.setObjectMapper(objectMapper); return serializer; } @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); //字串型別的資料的鍵為RedisSerializer序列化方式 template.setKeySerializer(RedisSerializer.string()); template.setHashKeySerializer(RedisSerializer.string()); //字串型別的資料的值的序列化方式為自定義 template.setValueSerializer(jsonSerializer()); template.setHashValueSerializer(jsonSerializer()); //spring規範中假如修改bean物件的預設特性,建議呼叫一下 template.afterPropertiesSet(); return template; }
}
RedisTemplate 工具類
package com.syh.pdd.configuration;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
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.RedisSerializer;@Configuration
public class RedisConfig {
//自定義序列化,反序列化物件public RedisSerializer jsonSerializer(){ Jackson2JsonRedisSerializer serializer =new Jackson2JsonRedisSerializer(Object.class); //基於此物件進行序列化的方式 ObjectMapper objectMapper = new ObjectMapper();
//表示get方法使用此序列化 //表示全部許可權的方法可以被序列化,預設public
objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.ANY);//修改序列化儲存型別,對想序列化會將物件的型別儲存到redis資料庫 //假如沒有這個配置,redis儲存時不儲存型別,反序列化預設儲存到map objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL,//啟用序列化型別儲存 JsonTypeInfo.As.PROPERTY);//表示型別會以json物件屬性形 式儲存 //物件屬性為空時,不進行序列化儲存 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); serializer.setObjectMapper(objectMapper); return serializer; } @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); //字串型別的資料的鍵為RedisSerializer序列化方式 template.setKeySerializer(RedisSerializer.string()); template.setHashKeySerializer(RedisSerializer.string()); //字串型別的資料的值的序列化方式為自定義 template.setValueSerializer(jsonSerializer()); template.setHashValueSerializer(jsonSerializer()); //spring規範中假如修改bean物件的預設特性,建議呼叫一下 template.afterPropertiesSet(); return template; }
}