Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply Spring Data's ReactiveCrudRepository to Redis

I'm playing with Spring Boot 2 with webflux. I'm trying to use ReactiveSortingRepository to simplify redis ops.

public interface DataProfileRepository extends ReactiveSortingRepository<DataProfileDTO, String> {
}

Simply use this interface

Mono<DataProfileDTO> tmp = this.dataProfileRepository.findById(id);

exception:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.tradeshift.dgps.dto.DataProfileDTO] to type [reactor.core.publisher.Mono<?>]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.data.repository.util.ReactiveWrapperConverters.toWrapper(ReactiveWrapperConverters.java:197) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutionResultHandler.postProcessInvocationResult(QueryExecutionResultHandler.java:104) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:587) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]

is thrown.

The behavior of this repository didn't match reactor, I can see in the debug mode, an actual DataProfileDTO was fetched from redis. And failed when trying to:

GENERIC_CONVERSION_SERVICE.convert(reactiveObject, targetWrapperType);

in ReactiveWrapperConverters.toWrapper

I went googling, it seems Spring Data Redis 2.0 doesn't mention reactive repository support. I'm wondering if anything I did wrong in my code or Spring Data Redis 2.0 just doesn't support ReactiveCrudRepository yet.

like image 765
HooYao Avatar asked Dec 28 '17 17:12

HooYao


People also ask

How do I add data to Redis cache in spring boot?

After adding the Redis dependencies, you now need to perform some configuration so that it could be used in your project. Spring Boot will automatically configure a Redis-cache Manager but with default properties. We can modify this configuration and change it as per our requirement.

Is Redis reactive?

Reactive Redis Template​ Spring Data Redis provides a Reactive API which plays well with all the other reactive parts of the Spring framework.

What is spring data Redis?

Spring Data Redis, part of the larger Spring Data family, provides easy configuration and access to Redis from Spring applications. It offers both low-level and high-level abstractions for interacting with the store, freeing the user from infrastructural concerns.


1 Answers

According to Spring's documentation for Reactive Redis Support, the highest level of abstraction to interface with Redis with reactive support is ReactiveRedisTemplate. The ReactiveRedisConnection is lower abstraction that works with binary values (ByteBuffer) as input and output.

There's no mention of support of reactive repositories. You can also consult the official reactive examples in the spring-data github repo.

In order for all this to work, you need to have reactive support in the driver you're using - currently that would be lettuce.

Although not ideal, an alternative is Flux.fromIterable(). You can use blocking repository and handle the result in reactive way.

public interface DataProfileRepository extends CrudRepository<DataProfileDTO, String> {
}

And wrap it:

Flux.fromIterable(dataProfileRepository.findById(id)), DataProfileDTO.class))
like image 141
hovanessyan Avatar answered Oct 13 '22 00:10

hovanessyan