redis版本的变更对于spring封装的java API影响不大,集成也没什么难度。重要的是序列化方面需要注意。
本次 spring-data-redis版本为2.5.5
spring-data-redis的序列化统统派生于 org.springframework.data.redis.serializer.RedisSerializer
接口
最终入redis都会是字节数组,但是不同的序列化器会进行不同的序列化策略,有的先转换jackson对象,再将该对象以字节数组的方式入redis。
// GenericToStringSerializer先将对象通过传唤器转换成字符串,再(指定编码)获得字节数组 @Override public T deserialize(@Nullable byte[] bytes) { if (bytes == null) { return null; } String string = new String(bytes, charset); return converter.convert(string, type); } @Override public byte[] serialize(@Nullable T object) { if (object == null) { return null; } String string = converter.convert(object, String.class); return string.getBytes(charset); }
// StringRedisSerializer直接进行字符串与字节数组(指定编码)转换操作,charset默认默认为UTF8 */ @Override public String deserialize(@Nullable byte[] bytes) { return (bytes == null ? null : new String(bytes, charset)); } /* * (non-Javadoc) * @see org.springframework.data.redis.serializer.RedisSerializer#serialize(java.lang.Object) */ @Override public byte[] serialize(@Nullable String string) { return (string == null ? null : string.getBytes(charset)); }
有时候我们想直接在redis相关的UI界面(RDM等)直观的看到数据,而我们的数组通常都是对象的形式存在,此时我们就可以使用先将原始java对象转为json对象,再由json对象转字节数组入redis
GenericJackson2JsonRedisSerializer
有个构造方法可以使用给定名称为默认类型配置ObjectMapper,或者配置默认的ObjectMapper
;Jackson2JsonRedisSerializer
不管ObjectMapper
怎么来的,只管用。GenericJackson2JsonRedisSerializer
构造方法里面使用默认ObjectMapper
的方式调用了过时方法,所以下文案例将采用Jackson2JsonRedisSerializer
org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Cannot construct instance of `org.springframework.security.authentication.UsernamePasswordAuthenticationToken` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
此时我们就可以使用JDK序列化,不便的是我们无法直接在redis UI界面直观的看数据值。
附上本人的redis配置
package com.example.demo; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @author ListJiang * @since 2021/10/21 * description redis配置 */ @Configuration public class RedisConfig { @Bean public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { // RedisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); // 序列化配置 RedisSerializer<Object> jdkSerializer = jdkSerializer(); RedisSerializer<String> stringSerializer = new StringRedisSerializer(); // String redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setValueSerializer(jdkSerializer); // Hash redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setHashValueSerializer(jdkSerializer); // 连接工厂配置 redisTemplate.setConnectionFactory(lettuceConnectionFactory); redisTemplate.afterPropertiesSet(); return redisTemplate; } private Jackson2JsonRedisSerializer jackson2JsonSerializer() { Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); return jackson2JsonRedisSerializer; } private JdkSerializationRedisSerializer jdkSerializer() { return new JdkSerializationRedisSerializer(); } }