欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Redission LocalDateTime序列化报错

程序员文章站 2022-06-28 16:29:46
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