1. 在 server模块的 pom文件中导入 spring data redis 依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-redis</artifactId> 4 </dependency>
2. 添加redis配置
application-dev.yml
sky: redis: host: localhost port: 6379 password: Redis密码
application.yml
spring: redis: host: ${sky.redis.host} port: ${sky.redis.port} password: ${sky.redis.password}
3. 配置类
1 package com.sky.config; 2 3 import com.fasterxml.jackson.annotation.JsonAutoDetect; 4 import com.fasterxml.jackson.annotation.PropertyAccessor; 5 import com.fasterxml.jackson.databind.ObjectMapper; 6 import com.fasterxml.jackson.databind.SerializationFeature; 7 import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; 8 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 9 import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; 10 import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; 11 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; 12 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; 13 import org.springframework.cache.annotation.CachingConfigurerSupport; 14 import org.springframework.context.annotation.Bean; 15 import org.springframework.context.annotation.Configuration; 16 import org.springframework.data.redis.connection.RedisConnectionFactory; 17 import org.springframework.data.redis.core.RedisTemplate; 18 import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; 19 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 20 import org.springframework.data.redis.serializer.RedisSerializer; 21 import org.springframework.data.redis.serializer.StringRedisSerializer; 22 23 import java.time.LocalDate; 24 import java.time.LocalDateTime; 25 import java.time.format.DateTimeFormatter; 26 27 // Redis配置类 28 @Configuration 29 public class RedisConfig extends CachingConfigurerSupport { 30 31 @Bean 32 @SuppressWarnings("all") 33 public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { 34 //原生RedisTemplate 35 RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); 36 template.setConnectionFactory(factory); 37 38 //json序列化 39 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = getJackson2JsonRedisSerializer(); 40 41 // -------- 设置key value 序列化方式 -------- 42 // key采用String的序列化方式 43 template.setKeySerializer(RedisSerializer.string()); 44 // hash的key也采用String的序列化方式 45 template.setHashKeySerializer(RedisSerializer.string()); 46 // value序列化方式采用jackson 47 template.setValueSerializer(jackson2JsonRedisSerializer); 48 // hash的value序列化方式采用jackson 49 template.setHashValueSerializer(jackson2JsonRedisSerializer); 50 template.afterPropertiesSet(); 51 return template; 52 } 53 54 private Jackson2JsonRedisSerializer getJackson2JsonRedisSerializer() { 55 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 56 ObjectMapper om = new ObjectMapper(); 57 //设置ObjectMapper访问权限 58 om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 59 //记录序列化之后的数据类型,方便反序列化 60 om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, 61 ObjectMapper.DefaultTyping.NON_FINAL); 62 63 //LocalDatetime序列化,默认不兼容jdk8日期序列化 64 JavaTimeModule timeModule = new JavaTimeModule(); 65 timeModule.addDeserializer(LocalDate.class, 66 new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); 67 timeModule.addDeserializer(LocalDateTime.class, 68 new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); 69 timeModule.addSerializer(LocalDate.class, 70 new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); 71 timeModule.addSerializer(LocalDateTime.class, 72 new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); 73 //关闭默认的日期格式化方式,默认UTC日期格式 yyyy-MM-dd’T’HH:mm:ss.SSS 74 om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); 75 om.registerModule(timeModule); 76 77 jackson2JsonRedisSerializer.setObjectMapper(om); 78 return jackson2JsonRedisSerializer; 79 } 80 }
标签:jackson,redis,环境,springframework,import,Redis,序列化,com,搭建 From: https://www.cnblogs.com/Rover20230226/p/17443419.html