当我想使用带有注释@Cacheable(spring cache)的findAll(Pageable pageable)时,无法反序列化org.springframework.data.domain.PageImpl?

时间:2019-12-30 02:54:00

标签: java spring caching

正如标题所述,

@CacheConfig(cacheNames = {"familyUserDao"})
public interface FamilyUserDao extends JpaRepository<FamilyUser,Long> {
    @Override
    @Cacheable(key = "methodName +#p0")
    Page<FamilyUser> findAll(Pageable pageable);
}

但是当我使用如下所示的方法时:

Page<FamilyUser> familyUserPage = familyUserDao.findAll(pageable);

错误消息:

org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Can not construct instance of org.springframework.data.domain.PageImpl: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: [B@7fae8571; line: 1, column: 46]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.springframework.data.domain.PageImpl: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: [B@7fae8571; line: 1, column: 46]
    at org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer.deserialize(Jackson2JsonRedisSerializer.java:73),

有没有好的建议?任何建议将被认真考虑。非常感谢你!

2 个答案:

答案 0 :(得分:0)

Jackson模块需要您提供一个与JSON文件的结构相匹配的反序列化类对象。解析错误几乎总是与以下任一相关:

  • 缺少或无法访问的类属性
  • 缺少构造函数或默认构造函数

我相信在您的情况下,您很可能会缺少适合于反序列化此JSON的构造函数。 (猜测您还没有提供PageImpl类的源代码)

您应该能够通过使用lombok的{​​{1}}或通过在PageImpl类内添加参数与所讨论的JSON中的字段匹配的新构造函数来解决此错误。

答案 1 :(得分:0)

  • DTO pojo类必须实现可序列化

    公共类FamilyUserDTO实现了Serializable {
           //成员
          //设置和获取

    }