HooYao
HooYao

Reputation: 564

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.

Upvotes: 16

Views: 6887

Answers (1)

hovanessyan
hovanessyan

Reputation: 31433

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))

Upvotes: 6

Related Questions