Spring-boot-redis序列化Date数据类型

org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Can not deserialize value of type java.util.Date from String “2017-8-30 12:00:00”: not a valid representation (error: Failed to parse Date value ‘2017-8-30 12:00:00’: Unparseable date: “2017-8-30 12:00:00”)

@Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        /**
         * om.setDateFormat(DateFormat.getDateTimeInstance());//格式化时间
         * 直接使用这种方式格式化时间的话,在Windows和在Linux上格式化后的时间格式是不一样的,
         * 因为DateFormat.getDateTimeInstance()具有默认语言环境的默认格式化风格。
         * Windows:2017-8-30 12:00:00
         * Linux:Aug 30, 2017 12:00:00 PM
         * 这样会出现这种情况:
         *      在Windows上序列化之后,在Linux上反序列化时识别不了,导致反序列化失败
         *      提示如下:
         *      org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Can not deserialize value of type java.util.Date from String "2017-8-30 12:00:00":
         *           not a valid representation (error: Failed to parse Date value '2017-8-30 12:00:00': Unparseable date: "2017-8-30 12:00:00")
         *  解决办法:
         *  om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
         */
//        om.setDateFormat(DateFormat.getDateTimeInstance());//格式化时间
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值