How to default RedisCacheManager to use Jackson annotations used on POJOs for @Cacheable methods

32 views Asked by At

I have this class hierarchy where B & C extends an abstract class A. Within A, I used the typical jackson annotations to tell jackson how to serialize & deserialize the polymorphic structure.

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "btType") // btType because we have a type field
@JsonIgnoreProperties(value = "btType", allowSetters = true)
@JsonSubTypes({@JsonSubTypes.Type(value = B.class, = "B")})
public abstract class A{}

We got a requirement to use the @Cacheable abstraction, but I was using RedisTemplate before that. I was able to successfully set and get my values just fine. jackson was successfully adding the btType field on serialization and it was deserializing B instances and a C instances.

I added this into my Jackson2JsonRedisSerializer which was used by the RedisCacheManager:

objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

When I introduced the @Cacheable annotation, I started getting deserialization errors:

com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class java.lang.Object]: missing type id property '@class'

It seemed like jackson was no longer looking for btType but instead @class. It occurred to me that my jackson annotations were interfering with the objectMapper in the RedisCacheManager because when I removed my jackson annotations on the POJO, everything worked without any other changes.

So my question is if there's a way to tell RedisCacheManager to use my jackson annotations on the POJOs instead of using the defaults of activateDefaultTyping? I feel like spring's abstraction should allow for this, but maybe I am just not using the right configurations.

It's not a big deal if I can't but I prefer my objects to not be stored in redis as @class: com.company.whatever.entity.B when it was btType: B with RedisTemplate.

I tried various different configurations within activateDefaultTyping() but none of them seemed to work.

0

There are 0 answers