Issue is when using Spring cache with redis cache manager, not able to deserializer Spring Pageable response due to no default constructor
The spring boot version used is 2.1.4.RELEASE
Redis config class that uses the serializer
@Bean
public RedisCacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues()
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
redisCacheConfiguration.usePrefix();
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory)
.cacheDefaults(redisCacheConfiguration).build();
}
I am trying to cache spring REST API Page result response in Redis cache using Spring cache and Redis as a cache backend
@GetMapping
@Cacheable("Article_Response_Page")
public Page<Article> findAll(Pageable pageable) {
return articleRepository.findAll(pageable);
}
I am able to see the Page<Article>
getting cached as JSON in Redis cache using RedisSerializer.json()
serializer but during the next call when the data is read from the cache I get the following exception
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
construct instance of `org.springframework.data.domain.PageImpl` (no
Creators, like default construct, exist): cannot deserialize from Object
value (no delegate- or property-based Creator)
at [Source: (byte[])"
{"@class":"org.springframework.data.domain.PageImpl","content":
["java.util.Collections$UnmodifiableRandomAccessList",[]],"pageable":
{"@class":"org.springframework.data.domain.PageRequest","sort":{"@class":"org.springframework.data.domain.Sort","sorted":false,"unsorted":true,"empty":true},"offset":0,"pageSize":20,"pageNumber":0,"paged":true,"unpaged":false},"totalPages":0,"totalElements":0,"last":true,"size":20,"number":0,"sort":{"@class":"org.springframework.data.domain.Sort","sorted":false,"uns"[truncated 73 bytes]; line: 1, column: 54]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) ~[jackson-databind-2.9.8.jar:2.9.8]
I tried giving a custom serializer for PageImpl then I got an exception for PageRequest implementation and Sort implementation all part of Spring 'org.springframework.data.domain' package
There must be a better way to solve this and I like to know the best approach to solve this kind of issue in spring cache
Is this a Jackson bug after moving to SPRING BOOT v2 ?