Spring之——RedisTemplate 序列化、反序列化扩展支持 FastJson:GenericFastJson2JsonRedisSerializer

转载请注明出处:https://blog.csdn.net/l1028386804/article/details/81713313

在Spring的RedisTemplate中如何使用FastJson来进行数据的序列化和反序列化操作,实在是一件头疼的事情,基于Java注解的配置相对来说比较简单,但是基于XML配置的方式确实让人有点。。。(此处省略几个字,自己脑补),经过笔者不断总结尝试,最终解决了如何在XML配置下,将FastJson作为RedisTemplate进行数据序列化和反序列化的框架,从而实现了数据的序列化和反序列化操作。特此记录。直入主题如下:

类GenericFastJson2JsonRedisSerializer

package com.utils.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.nio.charset.Charset;
/**
 * @author liuyazhuang
 * @date 2018/8/15 23:23
 * @description  自定义GenericFastJson2JsonRedisSerializer序列化器,其中用到了FastJsonWraper包装类
 * @version 1.0.0
 */
public class GenericFastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {
    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    public GenericFastJson2JsonRedisSerializer() {
        super();
    }
   @Override
    public byte[] serialize(T t) throws SerializationException {
        if (t == null) {
            return new byte[0];
        }
       FastJsonWraper<T> wraperSet =new FastJsonWraper<>(t);
       return JSON.toJSONString(wraperSet, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
   }
    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        String deserializeStr = new String(bytes, DEFAULT_CHARSET);
        FastJsonWraper<T> wraperGet=JSON.parseObject(deserializeStr,FastJsonWraper.class);
        return wraperGet.getValue();
    }
}

类FastJsonWraper

 package com.utils.redis;

/**
 * @author liuyazhuang
 * @date 2018/8/15 23:15
 * @description FastJsonWraper包装类
 * @version 1.0.0
 */
public class FastJsonWraper<T> {
    private T value;

    public FastJsonWraper() {
    }

    public FastJsonWraper(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}

spring-redis 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <description>Spring-redis</description>

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="50" />    <!--最大连接数-->
        <property name="maxIdle" value="10" />     <!--最大空闲数-->
        <property name="maxWaitMillis" value="3000" />    <!--最大等待时间ms-->
        <property name="testOnBorrow" value="true" />
    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="127.0.0.1" />
        <property name="port" value="9500"/>
        <property name="password" value="redis"/>
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="usePool" value="true"/>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory"   ref="jedisConnectionFactory" />
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="com.utils.redis.GenericFastJson2JsonRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="com.utils.redis.GenericFastJson2JsonRedisSerializer"/>
        </property>
    </bean>
</beans>

猜你喜欢

转载自blog.csdn.net/l1028386804/article/details/81713313