问题
I'm using spring boot 2.0.3 and spring-boot-starter-data-redis. Also using jackson-datatype-jsr310.
I want to store Object into redis.
the object(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:
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
But when I'm pushing into redis, the error occurs below:
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)
How to serialize java instant type with Redis?
Any opinion would be appreciated.
来源:https://stackoverflow.com/questions/50989621/how-to-serialize-java-instant-type-using-redis