问题是使用带有 redis 缓存管理器的 Spring 缓存时,由于没有默认构造函数,无法反序列化 Spring Pageable 响应
使用的spring boot版本是2.1.4.RELEASE
使用序列化程序的 Redis 配置类
@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();
}
我正在尝试使用 Spring 缓存和 Redis 作为缓存后端在 Redis 缓存中缓存 Spring REST API 页面结果响应
@GetMapping
@Cacheable("Article_Response_Page")
public Page<Article> findAll(Pageable pageable) {
return articleRepository.findAll(pageable);
}
我可以看到页面使用 RedisSerializer.json() 序列化程序在 Redis 缓存中缓存为 JSON,但是在下一次调用从缓存中读取数据时,我收到以下异常
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]
我尝试为 PageImpl 提供自定义序列化程序,然后我得到了 PageRequest 实现和 Sort 实现的异常,所有 Spring 'org.springframework.data.domain' 包的部分
必须有更好的方法来解决这个问题,我想知道在 spring 缓存中解决此类问题的最佳方法
这是迁移到 SPRING BOOT v2 后的 Jackson 错误吗?