赞
踩
因为fastjson升级到2后,AutoType默认是关闭的,需要添加类名到拦截器才可以正常转换类型,但是我们使用Fastjson2作为Redis的序列化工具时非常不方便!
解决AutoType无法自动转换导致报错
将类的类名和数据一同保存到redis中,需要转换时自动添加到拦截器
FastJson2RedisSerializer.java(fastjson2序列化与反序列化;实现RedisSerializer接口)
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONReader; import com.alibaba.fastjson2.JSONWriter.Feature; import java.nio.charset.Charset; import java.util.AbstractMap.SimpleEntry; import java.util.Map; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationException; /** * redis fastjson2 serializer. */ public class FastJson2RedisSerializer<T> implements RedisSerializer<T> { /** * cls. */ private final Class<T> clazz; /** * 序列化. * * @param t object to serialize. Can be {@literal null}. * @return es * @throws SerializationException e */ @Override public byte[] serialize(T t) throws SerializationException { if (t == null) { return new byte[0]; } Map.Entry<String, T> entity = new SimpleEntry<>(t.getClass().getName(), t); return JSON.toJSONString(entity, Feature.WriteClassName).getBytes(Charset.defaultCharset()); } /** * 反序列化. * * @param bytes object binary representation. Can be {@literal null}. * @return es * @throws SerializationException e */ @Override public T deserialize(byte[] bytes) throws SerializationException { if (bytes == null) { return null; } String str = new String(bytes, Charset.defaultCharset()); int index = str.indexOf(":"); String cls = str.substring(2, index - 1); String obj = str.substring(index + 1, str.length() - 1); return JSON.parseObject( obj, clazz, JSONReader.autoTypeFilter( cls ), JSONReader.Feature.SupportClassForName); } /** * 构造器. * * @param clazz cls */ public FastJson2RedisSerializer(Class<T> clazz) { super(); this.clazz = clazz; } }
2.应用到spring boot自动初始化的Redis
import com.simple.framework.data.redis.serializer.FastJson2RedisSerializer; import org.springframework.cache.annotation.CachingConfigurer; import org.springframework.cache.annotation.EnableCaching; 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.StringRedisSerializer; /** * redis cache configuration。 */ @Configuration @EnableCaching @SuppressWarnings("all") public class RedisConfig implements CachingConfigurer { /** * wirter RedisTemplate Bean。 * * @param connectionFactory * @return template */ @Bean public RedisTemplate<Object, ?> redisTemplate(final RedisConnectionFactory connectionFactory) { return createTemplate(connectionFactory); } /** * 创建redisTemplate. * * @param connectionFactory 连接工厂 * @return redisTemplate */ public static RedisTemplate<Object, ?> createTemplate( final RedisConnectionFactory connectionFactory) { RedisTemplate<Object, ?> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); FastJson2RedisSerializer serializer = new FastJson2RedisSerializer(Object.class); template.setValueSerializer(serializer); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(serializer); template.setHashValueSerializer(serializer); template.setDefaultSerializer(serializer); template.setStringSerializer(serializer); template.afterPropertiesSet(); return template; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。