Make JacksonJsonRedisSerializer NOT depend on a specific domain object class [DATAREDIS-175] #753
I am trying to use Redis with Spring (and) looking at the configuration required to acheive this, I've noticed that "JacksonJsonRedisSerializer" requires a domain object class for instantiation.
My application has multiple domain objects and i would like to enable redis caching on several rest API methods (say --- getObject1, getObject2 etc.). Each of these methods return a different object.
The default JDK serialization works fine (has to do with enough information stored for deserializing). However, JSON serialization using "Object" class doesn't work (i guess as expected).
Tried using "CompositeCacheManager" (one cacheManager per domain object that needs to be cached.....not elegant but gave it a try due to limitation with spring config) but am stuck due to https://jira.springsource.org/browse/SPR-8696
Please advise as to how to proceed. Appreciate all the help
0 votes, 6 watchers
The text was updated successfully, but these errors were encountered:
Jennifer Hickey commented
All RedisSerializers need to declare a specific type, so not sure we could really make the change you are asking for.
Until SPR-8696 is implemented, the main problem seems to be that RedisCacheManager.getCache() will always instantiate a new RedisCache instead of returning null, so CompositeCacheManager is not guaranteed to return a RedisCache with the correct RedisTemplate injected.
For example, consider the XML config for 2 domain objects, Person and Address. Each has its own RedisTemplate with its own instance of JacksonJsonRedisSerializer:
Unfortunately, with this configuration, the CompositeCacheManager will always return a RedisCache with the personRedisTemplate for the "address" cache. This results in deserialization errors when reading from the cache.
You could maybe work around this by implementing your own CompositeCacheManager or RedisCacheManager that can pick the correct redisTemplate based on the cache name (using reflection or bean name parsing, i.e. the "address" cache name should use the "addressRedisTemplate" bean in its RedisCache)
David Coleman commented
Can you simply not create your own implementation of RedisSerializer that will map to/from Json based upon object type? The code below seems to work.
Alex Paransky commented
Enabling default typing allows ObjectMapper to write and read arbitrary objects from the stream (similar to the way JavaSerializer works). The type of the object is written out so that when it's de-serialized ObjectMapper knows specifically which class to instantiate.
This is the reason, i think this ticket is closed. Enable default typing and you should be good to go with a single serializer that can serialize/deserialize any type of JSON object using Jackson's ObjectMapper