[英]How to serialize Java Instant type using redis
I'm using spring boot 2.0.3 and spring-boot-starter-data-redis.我正在使用 spring boot 2.0.3 和 spring-boot-starter-data-redis。 Also using jackson-datatype-jsr310.
也使用jackson-datatype-jsr310。
I want to store Object into redis.我想将 Object 存储到 redis 中。
the object(MyObj):对象(MyObj):
String text;
Instant instant;
Here's my code:这是我的代码:
@Test
public void test() {
ListOperations<String, MyObj> listOps = redisTemplate.opsForList();
MyObj o1 = new MyObj();
o1.setText("foo");
o1.setInstant(Instant.now());
listOps.leftPush("foo", o1);
MyObj o2 = new MyObj();
o2.setText("bar");
o2.setInstant(Instant.now());
listOps.leftPush("foo", o2);
List<MyObj> list = listOps.range("foo", 0, -1);
for (MyObj o : list) {
System.out.println(o.getText());
System.out.println(o.getInstant());
}
}
in my RedisConfig:在我的 RedisConfig 中:
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
But when I'm pushing into redis, the error occurs below:但是当我推入redis时,错误发生在下面:
org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Cannot construct instance of
java.time.Instant
(no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)org.springframework.data.redis.serializer.SerializationException:无法读取 JSON:无法构造
java.time.Instant
的实例(没有创建者,如默认构造,存在):无法从 Object 值反序列化(没有基于委托或基于属性的创作者)
How to serialize java instant type with Redis?如何使用 Redis 序列化 java 即时类型?
Any opinion would be appreciated.任何意见将不胜感激。
While this is quite an old post, I ran into this problem recently and did the lazy man search and found this before deciding to read the class file.虽然这是一篇相当老的帖子,但我最近遇到了这个问题,并在决定阅读类文件之前做了懒惰的人搜索并找到了这个。 I found that you can easily override the default
ObjectMapper
with a custom one.我发现您可以使用自定义对象轻松覆盖默认的
ObjectMapper
。 Use the setObjectMapper(ObjectMapper objectMapper)
method on the Serializer to override the default.使用 Serializer 上的
setObjectMapper(ObjectMapper objectMapper)
方法覆盖默认值。
// Taken from Jackson library
public class Jackson2JsonRedisSerializer<T> implements RedisSerializer<T> {
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private final JavaType javaType;
private ObjectMapper objectMapper = new ObjectMapper();
// truncated
public void setObjectMapper(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "'objectMapper' must not be null");
this.objectMapper = objectMapper;
}
You just need to create an ObjectMapper with the JavaTime.Module registered like below您只需要使用注册的 JavaTime.Module 创建一个 ObjectMapper,如下所示
public static ObjectMapper dateMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return mapper;
Jackson2JsonRedisSerializer<MyObj> valueSerializer = new
Jackson2JsonRedisSerializer<>(MyObj.class);
valueSerializer.setObjectMapper(dateMapper());
}
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
turns of default behavior. disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
轮流默认行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.