How do I serialize/deserialize a Spring Data Window<T> with Jackson

55 views Asked by At

I am trying to cache a Spring Data Window<T> using the Spring Cache abstraction with Redis. The problem arises during the deserialization process after the cache hits."

org.springframework.data.redis.serializer.SerializationException: Could not read JSON:
Cannot construct instance of `org.springframework.data.domain.WindowImpl` (no Creators, like default constructor, exist):
cannot deserialize from Object value (no delegate- or property-based Creator)

How can I create a serializable/deserializable DTO from Window<T> that includes a KeysetScrollPosition.

@Configuration
public class RedisConfiguration {
    @Bean
    public RedisCacheConfiguration cacheConfiguration() {
        return RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(60))
                .disableCachingNullValues()
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
    }
}
public interface PersonRepository extends Repository<PersonEntity, String> { 
  Window<PersonEntity> findAllBy(ScrollPosition position, Limit limit);
}
@Controller
public class PersonController {

    @Cacheable(value = "persons", key = "#root.methodName + #subrange.position + #subrange.count")
    @QueryMapping("findAllPersons")
    public Window<PersonEntity> findAll(ScrollSubrange subrange) {
      ScrollPosition position = subrange.position().orElse(ScrollPosition.keyset());
      return personRepository.findAllBy(position, Limit.of(subrange.count().getAsInt())));
    }
}
0

There are 0 answers