阿里巴巴redis的序列化缓存方法及解决办法!(二)

在处理项目时,您需要缓存发票数据。在redis中,你想把它存储在json数据中。但是在获取数据的时候会报错:

org..data.redis..on: 无法读取 JSON: token (),:’@class’ 即输入 id(对于 java.lang 类。)

在 [: [B@;行:1,:5611]; is com….: token(),:’@class’ 即输入id(用于java.lang类。)

在 [: [B@;行:1,:5611]

可能是redis的序列化错误,就是这个id类型的属性缺失,映射错误。

现在我们来看看我们redis里面的数据是什么样子的:注意这里的属性开发票 object error是什么意思,属性的位置顺序不对。

图片[1]-阿里巴巴redis的序列化缓存方法及解决办法!(二)-4747i站长资讯

这是我的属性顺序。请与上面的json数据对比:有问题吗?上面的json数据还没有截获,但是主要问题是属性顺序颠倒了!这里的@注解是我测试阿里的设置,可以忽略。实际开发中可以忽略这一点!

图片[2]-阿里巴巴redis的序列化缓存方法及解决办法!(二)-4747i站长资讯

因此:使用序列化的json作为redis确实存在一些隐藏的问题!

解决方案:使用阿里巴巴的序列化工具redis!

主要配置如下,我直接在代码里配置了!

package com.ilongsay.redisConfig;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
 * @Author ilongsay
 * @Email ilongsay@163.com
 * @Copyright 版本所有,侵权必究!
 * @Datetime 2019-02-25 22:58
 * @Describution
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @param connectionFactory 这个报错不影响使用
     * @return org.springframework.data.redis.core.RedisTemplate
     *
     */
    @Bean(name = "redisTemplate")
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
        connectionFactory = jedisConnectionFactory;
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(connectionFactory);
        //使用Fastjson2JsonRedisSerializer序列化value的值
        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);
        template.setValueSerializer(serializer);
        //使用StringRedisSerializer序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
} 

再次运行获取value的值开发票 object error是什么意思,完美的得到不报错,就好了。

文章来源:https://blog.csdn.net/weixin_38238552/article/details/88038925

------本页内容已结束,喜欢请分享------

感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享