SpringBoot注解@Cacheable与Redis缓存关于LocalDateTime。。。

更新时间:2023-05-21 12:55:15 阅读: 评论:0

SpringBoot注解@Cacheable与Redis缓存关于
LocalDateTime。。。
由于项⽬中使⽤Spring Boot 2.0,以及需要将包含LocalDateTime的对象通过注解@Cacheable存⼊缓存。若不做配置,存⼊缓存会成功(redis数据类型为HEX,不便阅读),取出对象时则会报错反序列化失败。下⾯是具体解决办法:
需要增加⼀个修改RedisCacheManager序列化格式的RedisConfig类,包含LocalDateTime类型属性不需要添加任何注解。
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.dis.cache.RedisCacheConfiguration;
import org.dis.cache.RedisCacheManager;
import org.dis.cache.RedisCacheWriter;
import org.tion.RedisConnectionFactory;
import org.RedisTemplate;
import org.dis.rializer.Jackson2JsonRedisSerializer;
import org.dis.rializer.RedisSerializationContext;
import org.dis.rializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.der.LocalDateDerializer;
import com.fasterxml.jackson.datatype.jsr310.der.LocalDateTimeDerializer;
高级英语第一册课后答案import com.fasterxml.jackson.datatype.jsr310.der.LocalTimeDerializer;
import com.fasterxml.jackson.LocalDateSerializer;
import com.fasterxml.jackson.LocalDateTimeSerializer;
import com.fasterxml.jackson.LocalTimeSerializer;
@Configuration
public class RedisConfig {
/
** 默认⽇期时间格式 */
private static final String    DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/** 默认⽇期格式 */
private static final String    DEFAULT_DATE_FORMAT      = "yyyy-MM-dd";
/** 默认时间格式 */
private static final String    DEFAULT_TIME_FORMAT      = "HH:mm:ss";
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
Jackson2JsonRedisSerializer<Object> rializer = new Jackson2JsonRedisSerializer<Object>(
Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.tVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//LocalDateTime系列序列化和反序列化模块,继承⾃jsr310,我们在这⾥修改了⽇期格式
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(
DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
javaTimeModule.addSerializer(LocalDate.class,
new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
javaTimeModule.addSerializer(LocalTime.class,
javaTimeModule.addSerializer(LocalTime.class,
new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
javaTimeModule.addDerializer(LocalDateTime.class, new LocalDateTimeDerializer(
DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
javaTimeModule.addDerializer(LocalDate.class,
new LocalDateDerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
javaTimeModule.addDerializer(LocalTime.class,
new LocalTimeDerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
rializer.tObjectMapper(objectMapper);
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.tConnectionFactory(redisConnectionFactory);
redisTemplate.tKeySerializer(new StringRedisSerializer());
redisTemplate.tValueSerializer(rializer);
redisTemplate.tHashKeySerializer(new StringRedisSerializer());
redisTemplate.tHashValueSerializer(rializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public RedisCacheManager redisCacheManager(RedisTemplate<String, Object> redisTemplate) {
RedisCacheWriter redisCacheWriter = RedisCacheWriter
.
ConnectionFactory());英汉对照新闻
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
.defaultCacheConfig().rializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate
.getValueSerializer()));
return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
}
}
⾄此,存取缓存均⽆问题,且redis数据类型为Plain Text。
另外,附加⼀个Controller对外统⼀格式化时间类型的配置,通过这样的配置可以实现,所有进出Controller的时间格式均按照默认格式处理,不需要单独在LocalDateTime属性上增加格式化注解。
import com.JsonGenerator;
import com.JsonParr;
import com.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.der.LocalDateDerializer;
import com.fasterxml.jackson.datatype.jsr310.der.LocalDateTimeDerializer;
import com.fasterxml.jackson.datatype.jsr310.der.LocalTimeDerializer;
import com.fasterxml.jackson.LocalDateSerializer;
import com.fasterxml.jackson.LocalDateTimeSerializer;
import com.fasterxml.jackson.LocalTimeSerializer;
import t.annotation.Bean;
import t.annotation.Configuration;
import verter.Converter;
import java.io.IOException;
ParException;
SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
少儿英语单词游戏
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@Configuration
public class DateConfig {
/** 默认⽇期时间格式 */
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/** 默认⽇期格式 */
matt
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
/** 默认时间格式 */
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
/**
* LocalDate转换器,⽤于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDate> localDateConverter() {
return new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
return LocalDate.par(source, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
}
};
}
/**
* LocalDateTime转换器,⽤于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDateTime> localDateTimeConverter() {
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String source) {
return LocalDateTime.par(source, DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));            }
};
}
/**
* LocalTime转换器,⽤于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalTime> localTimeConverter() {
return new Converter<String, LocalTime>() {
@Override
public LocalTime convert(String source) {
return LocalTime.par(source, DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT));
}
};
}
/**
小考分数查询* Date转换器,⽤于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, Date> dateConverter() {
return new Converter<String, Date>() {
@Override
public Date convert(String source) {
SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
try {
return format.par(source);
} catch (ParException e) {
一般现在时的结构throw new RuntimeException(e);
}
}
};
};
}
/**
* Json序列化和反序列化转换器,⽤于转换Post请求体中的json以及将我们的对象序列化为返回响应的json
*/
@Bean
public ObjectMapper objectMapper(){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(DerializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
国名教育
//LocalDateTime系列序列化和反序列化模块,继承⾃jsr310,我们在这⾥修改了⽇期格式
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
javaTimeModule.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
javaTimeModule.addSerializer(LocalTime.class,new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
javaTimeModule.addDerializer(LocalDateTime.class,new LocalDateTimeDerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));        javaTimeModule.addDerializer(LocalDate.class,new LocalDateDerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
javaTimeModule.addDerializer(LocalTime.class,new LocalTimeDerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
//Date序列化和反序列化
javaTimeModule.addSerializer(Date.class, new JsonSerializer<Date>() {
@Override
public void rialize(Date date, JsonGenerator jsonGenerator, SerializerProvider rializerProvider) throws IOException {
SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
小学快乐英语
String formattedDate = formatter.format(date);
jsonGenerator.writeString(formattedDate);
}
});
javaTimeModule.addDerializer(Date.class, new JsonDerializer<Date>() {
laura story grace
@Override
public Date derialize(JsonParr jsonParr, DerializationContext derializationContext) throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
String date = Text();
try {
return format.par(date);
} catch (ParException e) {
throw new RuntimeException(e);
}
}
});
return objectMapper;
}
}
past

本文发布于:2023-05-21 12:55:15,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/719726.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:序列化   格式   时间   默认
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图