问题是在使用redis Cache Manager的Spring Cache时,由于没有默认的构造函数
,因此无法获得spring pag -pag -pag -pagable响应。使用的弹簧引导版本为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 Cache和Redis作为缓存后端
来缓存spring rest api页面结果响应redis缓存中的响应@GetMapping
@Cacheable("Article_Response_Page")
public Page<Article> findAll(Pageable pageable) {
return articleRepository.findAll(pageable);
}
我能够看到Page<Article>
使用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实现的例外,并将实现分类为Spring'org.springframework.data.data.domain'package'package
必须有一种更好的方法来解决这个问题,我喜欢知道解决此类问题的最佳方法
这是杰克逊(Jackson(的错误,是在转到Spring Boot V2之后?
我在创建一个简单的REST接口时也有相同的问题。对我来说,解决方案是扩展PageImpl并指定所需的JSONPROPERTIES,同时明确忽略其他jsonproperties:
@JsonIgnoreProperties(ignoreUnknown = true, value = {"pageable"})
public class RestPage<T> extends PageImpl<T> {
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public RestPage(@JsonProperty("content") List<T> content,
@JsonProperty("number") int page,
@JsonProperty("size") int size,
@JsonProperty("totalElements") long total) {
super(content, PageRequest.of(page, size), total);
}
public RestPage(Page<T> page) {
super(page.getContent(), page.getPageable(), page.getTotalElements());
}
}
您可以沿着:
的行写控制器@GetMapping
@Cacheable("Article_Response_Page")
public RestPage<Article> findAll(Pageable pageable) {
return new RestPage<>(articleRepository.findAll(pageable));
}
我正在使用Spring Boot 2.6.2。试图将HTTP获取请求到返回页面的Spring Boot Service,遇到了同样的错误。我只是通过在我的假句子中添加属性feign.autoconfiguration.jackson.enabled=true
来解决它。我正在使用spring-cloud-starter-openfeign。
https://docs.spring.io/spring-cloud-openfeign/docs/current/referent/Reference/html/#spring-data-support
您可以使用pageimpl的包装器,然后:
public class PageImpl<T> extends org.springframework.data.domain.PageImpl<T> {
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public PageImpl(@JsonProperty("content") List<T> content,
@JsonProperty("number") int page,
@JsonProperty("size") int size,
@JsonProperty("totalElements") long total) {
super(content, PageRequest.of(page, size), total);
}
}
我通过使用Java Serializer暂时解决了问题
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues()
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.java()));
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new PageJacksonModule());
}