From 5f584e683aec0582e126c8e8049ef8d658880a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Mon, 24 May 2021 16:12:40 +0800 Subject: [PATCH] =?UTF-8?q?update=20redis=E5=BA=8F=E5=88=97=E5=8C=96?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA=E4=BD=BF=E7=94=A8jackson?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/FastJson2JsonRedisSerializer.java | 71 ------------------- .../ruoyi/framework/config/RedisConfig.java | 5 +- 2 files changed, 3 insertions(+), 73 deletions(-) delete mode 100644 ruoyi-framework/src/main/java/com/ruoyi/framework/config/FastJson2JsonRedisSerializer.java diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/FastJson2JsonRedisSerializer.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/FastJson2JsonRedisSerializer.java deleted file mode 100644 index 59812ea5..00000000 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/FastJson2JsonRedisSerializer.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.ruoyi.framework.config; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.serializer.SerializerFeature; -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.type.TypeFactory; -import org.springframework.data.redis.serializer.RedisSerializer; -import org.springframework.data.redis.serializer.SerializationException; -import com.alibaba.fastjson.parser.ParserConfig; -import org.springframework.util.Assert; -import java.nio.charset.Charset; - -/** - * Redis使用FastJson序列化 - * - * @author ruoyi - */ -public class FastJson2JsonRedisSerializer implements RedisSerializer -{ - @SuppressWarnings("unused") - private ObjectMapper objectMapper = new ObjectMapper(); - - public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); - - private Class clazz; - - static - { - ParserConfig.getGlobalInstance().setAutoTypeSupport(true); - } - - public FastJson2JsonRedisSerializer(Class clazz) - { - super(); - this.clazz = clazz; - } - - @Override - public byte[] serialize(T t) throws SerializationException - { - if (t == null) - { - return new byte[0]; - } - return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET); - } - - @Override - public T deserialize(byte[] bytes) throws SerializationException - { - if (bytes == null || bytes.length <= 0) - { - return null; - } - String str = new String(bytes, DEFAULT_CHARSET); - - return JSON.parseObject(str, clazz); - } - - public void setObjectMapper(ObjectMapper objectMapper) - { - Assert.notNull(objectMapper, "'objectMapper' must not be null"); - this.objectMapper = objectMapper; - } - - protected JavaType getJavaType(Class clazz) - { - return TypeFactory.defaultInstance().constructType(clazz); - } -} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java index a65626aa..b0f4b989 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java @@ -6,6 +6,7 @@ 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; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonTypeInfo; @@ -15,7 +16,7 @@ import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator /** * redis配置 - * + * * @author ruoyi */ @Configuration @@ -29,7 +30,7 @@ public class RedisConfig extends CachingConfigurerSupport RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); - FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class); + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);