解決redis序列化java8 LocalDateTime

redis 序列化問題

java

在 JDK8 中 官方提出 LocalDateTime 的時間格式,由於他是線程安全的,redis

spring

在咱們的業務中,咱們須要把 一些數據存到緩存中,不可避免的存 LocalDateTime 格式的時間,相信許多小夥伴都有遇到 存取 出現 LocalDateTime序列化異常緩存

安全

在咱們實際開發中 LocalDateTime格式 會有 T 下面序列化咱們把 T 業序列化app

解決方案

1. 使用註解的方式

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonFormat( pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateDate;



2. 使用全局配置文件

package com.yxl.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* @author yxl
* @date 2020-05-07 11:52
*/

@Configuration
public class RedisConfig {

   //過時時間-1天
   private Duration timeToLive = Duration.ofDays(-1);



   /**
    * RedisTemplate 先關配置
    *
    * @param factory
    * @return
    */

   @Bean
   @SuppressWarnings("all")
   public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
       RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
       template.setConnectionFactory(factory);
       Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
       ObjectMapper om = new ObjectMapper();
       om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
       om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

       //LocalDatetime序列化
       JavaTimeModule timeModule = new JavaTimeModule();
       timeModule.addDeserializer(LocalDate.class,
               new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
       timeModule.addDeserializer(LocalDateTime.class,
               new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
       timeModule.addSerializer(LocalDate.class,
               new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
       timeModule.addSerializer(LocalDateTime.class,
               new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

       om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
       om.registerModule(timeModule);

       jackson2JsonRedisSerializer.setObjectMapper(om);
       StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
       // key採用String的序列化方式
       template.setKeySerializer(stringRedisSerializer);
       // hash的key也採用String的序列化方式
       template.setHashKeySerializer(stringRedisSerializer);
       // value序列化方式採用jackson
       template.setValueSerializer(jackson2JsonRedisSerializer);
       // hash的value序列化方式採用jackson
       template.setHashValueSerializer(jackson2JsonRedisSerializer);
       template.afterPropertiesSet();
       return template;
   }


   @Bean
   public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
       //默認1
       RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
               .entryTtl(timeToLive)
               .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
               .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()))
               .disableCachingNullValues();
       RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory)
               .cacheDefaults(config)
               .transactionAware()
               .build();
       return redisCacheManager;
   }

   /**
    * key 類型
    * @return
    */

   private RedisSerializer<String> keySerializer() {
       return  new StringRedisSerializer();
   }

   /**
    * 值採用JSON序列化
    * @return
    */

   private RedisSerializer<Object> valueSerializer() {
       return new GenericJackson2JsonRedisSerializer();
   }


}

查看咱們的redis 成功解決ide

圖片

相關文章
相關標籤/搜索