How to serialize Java Instant type using redis

笑着哭i 提交于 2019-12-10 18:06:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!