Spring-boot-redis序列化Date数据类型
2017-08-30 17:53
1566 查看
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; }
相关文章推荐
- spring boot中关于redis 保存数据的序列化(数据库中的乱码问题)
- redis中的基本数据类型,以及在Spring-Boot对Redis的基本使用
- springboot + redis 操作几种数据类型
- redis中的基本数据类型,以及在Spring-Boot对Redis的基本使用
- spring redisTemplate 持久化到redis中数据更改为String类型
- 7、redis之使用spring集成commons-pool来操作常见数据类型
- springmvc/springboot处理前台字符串日期自动转换成后台date类型的三种办法
- 4 Springboot中使用redis存储集合数据,并模拟条件查询、分页读取
- spring boot data RedisTemplate 序列化问题
- Spring注入Date类型的数据到Bean中
- spring boot 框架下前端传递时间映射到后端的Date类型问题
- spring boot返回date类型给前端,显示的是毫秒的问题
- Spring Boot使用redis做数据缓存
- spring 注入Date 类型的数据
- Spring依赖注入对于Date类型数据的处理
- SpringBoot整合Redis(附带序列化方式对比)
- springboot form表单中 直接传递 Date类型
- redis学习(二)redis数据类型 Date Types
- org.springframework.jdbc.UncategorizedSQLException: 不允许从数据类型 varbinary 到 date 的隐式转换
- spring boot 整合redis对查询数据做缓存( 利用spring的AOP技术)