redis时间序列化
This commit is contained in:
parent
5cb28e4371
commit
576efa234a
@ -1,6 +1,10 @@
|
||||
package com.cdzy.operations.config;
|
||||
|
||||
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.LocalDateTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@ -15,8 +19,11 @@ import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* Redis配置类
|
||||
* @author attiya
|
||||
* @since 2025-03-20
|
||||
*/
|
||||
@ -47,6 +54,43 @@ public class RedisConfig {
|
||||
@Value("${spring.data.redis.lettuce.pool.max-wait}")
|
||||
private String maxWait;
|
||||
|
||||
/**
|
||||
* 创建Jackson序列化器,支持LocalDateTime
|
||||
*/
|
||||
private Jackson2JsonRedisSerializer<Object> createJacksonSerializer() {
|
||||
Jackson2JsonRedisSerializer<Object> serializer =
|
||||
new Jackson2JsonRedisSerializer<>(Object.class);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
// 注册JavaTimeModule支持Java8日期时间类型
|
||||
JavaTimeModule javaTimeModule = new JavaTimeModule();
|
||||
|
||||
// 为LocalDateTime配置序列化和反序列化器(使用自定义格式)
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
javaTimeModule.addSerializer(LocalDateTime.class,
|
||||
new LocalDateTimeSerializer(formatter));
|
||||
javaTimeModule.addDeserializer(LocalDateTime.class,
|
||||
new LocalDateTimeDeserializer(formatter));
|
||||
|
||||
mapper.registerModule(javaTimeModule);
|
||||
|
||||
// 禁用日期时间序列化为时间戳格式
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
|
||||
// 设置时区
|
||||
// mapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
||||
|
||||
// 保留原有的默认类型激活设置
|
||||
mapper.activateDefaultTyping(
|
||||
mapper.getPolymorphicTypeValidator(),
|
||||
ObjectMapper.DefaultTyping.NON_FINAL
|
||||
);
|
||||
|
||||
serializer.setObjectMapper(mapper);
|
||||
return serializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认数据库的连接工厂(数据库1)
|
||||
*/
|
||||
@ -119,16 +163,13 @@ public class RedisConfig {
|
||||
/**
|
||||
* 创建并配置RedisTemplate的通用方法
|
||||
*/
|
||||
private RedisTemplate<String, Object> createAndConfigureRedisTemplate(RedisConnectionFactory factory) {
|
||||
private RedisTemplate<String, Object> createAndConfigureRedisTemplate(
|
||||
RedisConnectionFactory factory,
|
||||
Jackson2JsonRedisSerializer<Object> serializer) {
|
||||
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(factory);
|
||||
|
||||
// 使用Jackson2JsonRedisSerializer序列化值
|
||||
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
serializer.setObjectMapper(mapper);
|
||||
|
||||
// 设置键和值的序列化器
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(serializer);
|
||||
@ -143,19 +184,30 @@ public class RedisConfig {
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认RedisTemplate(数据库1)- 已添加序列化配置
|
||||
* 默认RedisTemplate(数据库1)- 已修复LocalDateTime序列化问题
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
public RedisTemplate<String, Object> redisTemplate() {
|
||||
return createAndConfigureRedisTemplate(defaultRedisConnectionFactory());
|
||||
Jackson2JsonRedisSerializer<Object> serializer = createJacksonSerializer();
|
||||
return createAndConfigureRedisTemplate(defaultRedisConnectionFactory(), serializer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库2的RedisTemplate - 已添加序列化配置
|
||||
* 数据库2的RedisTemplate - 已修复LocalDateTime序列化问题
|
||||
*/
|
||||
@Bean("redisTemplateDb2")
|
||||
public RedisTemplate<String, Object> redisTemplateDb2() {
|
||||
return createAndConfigureRedisTemplate(redisConnectionFactoryDb2());
|
||||
Jackson2JsonRedisSerializer<Object> serializer = createJacksonSerializer();
|
||||
return createAndConfigureRedisTemplate(redisConnectionFactoryDb2(), serializer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库0的RedisTemplate - 新增,如果需要使用数据库0
|
||||
*/
|
||||
@Bean("redisTemplateDb0")
|
||||
public RedisTemplate<String, Object> redisTemplateDb0() {
|
||||
Jackson2JsonRedisSerializer<Object> serializer = createJacksonSerializer();
|
||||
return createAndConfigureRedisTemplate(redisConnectionFactoryDb0(), serializer);
|
||||
}
|
||||
}
|
||||
@ -117,6 +117,12 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>${jackson.version}</version> <!-- 请使用与您项目中Jackson-core匹配的版本 -->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<profiles>
|
||||
<!--开发环境-->
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
package com.cdzy.report.config;
|
||||
|
||||
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.LocalDateTimeDeserializer;
|
||||
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.connection.RedisConnectionFactory;
|
||||
@ -8,23 +12,55 @@ import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* Redis配置类
|
||||
* @author attiya
|
||||
* @since 2025-03-20
|
||||
*/
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
|
||||
// 定义日期时间格式(可选)
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(factory);
|
||||
|
||||
// 使用Jackson2JsonRedisSerializer序列化值
|
||||
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
||||
Jackson2JsonRedisSerializer<Object> serializer =
|
||||
new Jackson2JsonRedisSerializer<>(Object.class);
|
||||
|
||||
// 配置ObjectMapper
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
|
||||
// 注册JavaTimeModule支持Java8日期时间类型
|
||||
JavaTimeModule javaTimeModule = new JavaTimeModule();
|
||||
|
||||
javaTimeModule.addSerializer(LocalDateTime.class,
|
||||
new LocalDateTimeSerializer(DATE_TIME_FORMATTER));
|
||||
javaTimeModule.addDeserializer(LocalDateTime.class,
|
||||
new LocalDateTimeDeserializer(DATE_TIME_FORMATTER));
|
||||
|
||||
mapper.registerModule(javaTimeModule);
|
||||
|
||||
|
||||
mapper.registerModule(javaTimeModule);
|
||||
|
||||
// 禁用日期时间序列化为时间戳格式
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
|
||||
// 保留原有的默认类型激活设置(用于反序列化时识别具体类型)
|
||||
mapper.activateDefaultTyping(
|
||||
mapper.getPolymorphicTypeValidator(),
|
||||
ObjectMapper.DefaultTyping.NON_FINAL
|
||||
);
|
||||
|
||||
serializer.setObjectMapper(mapper);
|
||||
|
||||
// 设置键和值的序列化器
|
||||
@ -36,4 +72,4 @@ public class RedisConfig {
|
||||
template.afterPropertiesSet();
|
||||
return template;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user