Redission LocalDateTime序列化报错
程序员文章站
2022-03-26 17:17:24
Redission配置LocalDateTime序列化和反序列化配置序列化类将序列化类配置到RedissionClient中配置序列化类public class JavaTimeModule extends SimpleModule { public JavaTimeModule() { super(PackageVersion.VERSION); this.addSerializer(LocalDateTime.class, new LocalDateTime...
Redission LocalDateTime序列化报错
配置序列化类
public class JavaTimeModule extends SimpleModule {
public JavaTimeModule() {
super(PackageVersion.VERSION);
this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
this.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
this.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
}
}
将序列化类配置到RedissionClient中
/**
* 注入redisson 客户端
* @return
*/
@Bean(destroyMethod = "shutdown", name = "redissonClient")
public RedissonClient useSingleServer(){
Config config = new Config();
config.useSingleServer().setAddress("redis://" + redissonProperties.getServerAdd());
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 解决jackson2无法反序列化LocalDateTime的问题
om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
om.registerModule(new JavaTimeModule());
config.setCodec(new JsonJacksonCodec(om));
RedissonClient redissonClient = Redisson.create(config);
return redissonClient;
}
配置之后不需要做其他操作,在用RedissionClient操作redis的时候,LocalDateTime会自动序列化转换
本文地址:https://blog.csdn.net/qq_38687308/article/details/110236924
上一篇: 【JVM学习】垃圾收集器与内存分配策略
下一篇: java语言final关键字
推荐阅读
-
springboot~mybatis里localdatetime序列化问题
-
springboot LocalDateTime 反序列化
-
springboot LocalDateTime 序列化、反序列化全局配置
-
Redission LocalDateTime序列化报错
-
spring boot redis 序列化报错 as a subtype of [simple type, class java.lang.Object]: no such class found
-
gson反序列化localdateTime格式化
-
使用JSON JavaScriptSerializer进行反序列化和序列化时报错,字符的长度超出了MaxJsonLength的长度
-
处理 字符串反序列化 报错
-
php unserialize反序列化报错问题
-
php unserialize反序列化报错问题