Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2206

ObjectMapper Redis Serialization Issue

$
0
0

I am trying to cache An entity like this:

class Foo {    String attr1;    Set<Long> attr2;    Set<Long> attr3;}

My ObjectMapper + Serializer Configuration:

  @Bean  public RedisSerializer<Object> serializer() {    ObjectMapper mapper = new ObjectMapper();    mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator());    mapper.registerModule(new Jdk8Module());    mapper.registerModule(new JavaTimeModule());    mapper.addMixIn(Collection.class, HibernateCollectionMixIn.class);    return new GenericJackson2JsonRedisSerializer(mapper);  }

With this configuration, my entity is stored in Redis like this:

{"attr1": "123456789","attr2": ["java.util.HashSet",    [      ["java.lang.Long",        1      ],      ["java.lang.Long",        2      ],      ["java.lang.Long",        3      ]    ]  ],"attr3": ["java.util.HashSet",    []  ]}

But when trying to read back from redis, my code fails with following exception:

org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class java.lang.Object at [Source: (byte[])"{"attr1":"123456789","attr2":["java.util.HashSet",[["java.lang.Long",1],["java.lang.Long",2],["java.lang.Long",3]]],"attr3":["java.util.HashSet",[]]}"; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class java.lang.Object at [Source: (byte[])"{"attr1":"123456789","attr2":["java.util.HashSet",[["java.lang.Long",1],["java.lang.Long",2],["java.lang.Long",3]]],"attr3":["java.util.HashSet",[]]}"; line: 1, column: 1]    at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:152)    at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:130)    at org.springframework.data.redis.serializer.DefaultRedisElementReader.read(DefaultRedisElementReader.java:49)    at org.springframework.data.redis.serializer.RedisSerializationContext$SerializationPair.read(RedisSerializationContext.java:272)    at org.springframework.data.redis.cache.RedisCache.deserializeCacheValue(RedisCache.java:280)    at org.springframework.data.redis.cache.RedisCache.lookup(RedisCache.java:94)    at org.springframework.cache.support.AbstractValueAdaptingCache.get(AbstractValueAdaptingCache.java:58)    at org.springframework.cache.interceptor.AbstractCacheInvoker.doGet(AbstractCacheInvoker.java:73)    at org.springframework.cache.interceptor.CacheAspectSupport.findInCaches(CacheAspectSupport.java:571)    at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:536)    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:402)    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:345)    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:64)

If I remove default typing from ObjectMapper, exception changes to :

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.foo.Foo    at com.sun.proxy.$Proxy190.findByAuuid(Unknown Source)    at com.foo.FooService.getFoo(FooServiceImpl.java:80)    at com.foo.FooService.getFooImpl$$FastClassBySpringCGLIB$$349e7f48.invoke(<generated>)    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)

What should I do so that I can de serialize this without problems.

TIA


Viewing all articles
Browse latest Browse all 2206

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>