将Spring Data的ReactiveCrudRepository应用于Redis

Hoo*_*Yao 14 spring-data-redis reactive

我正在玩Spring Boot 2 webflux.我正在尝试使用它ReactiveSortingRepository来简化redis操作.

public interface DataProfileRepository extends ReactiveSortingRepository<DataProfileDTO, String> {
}
Run Code Online (Sandbox Code Playgroud)

只需使用此界面即可

Mono<DataProfileDTO> tmp = this.dataProfileRepository.findById(id);
Run Code Online (Sandbox Code Playgroud)

例外:

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]
Run Code Online (Sandbox Code Playgroud)

被扔了.

此存储库的行为与reactor不匹配,我可以在调试模式中看到,实际DataProfileDTO是从redis获取的.尝试时失败了:

GENERIC_CONVERSION_SERVICE.convert(reactiveObject, targetWrapperType);
Run Code Online (Sandbox Code Playgroud)

ReactiveWrapperConverters.toWrapper

我去谷歌搜索,似乎Spring Data Redis 2.0没有提到反应式存储库支持.我想知道我的代码或Spring Data Redis 2.0中是否有任何错误,我还不支持ReactiveCrudRepository.

hov*_*yan 5

根据 Spring 的Reactive Redis Support文档,与具有反应性支持的 Redis 接口的最高抽象级别是ReactiveRedisTemplate. 的ReactiveRedisConnection是低级抽象与二进制值(字节缓冲区)作为输入和输出的工作原理。

没有提到对反应式存储库的支持。您也可以参考spring-data github repo 中的官方响应式示例。

为了使所有这些工作,您需要在您使用的驱动程序中获得反应式支持 - 目前是lettuce

虽然不理想,但另一种选择是Flux.fromIterable(). 您可以使用阻塞存储库并以反应方式处理结果。

public interface DataProfileRepository extends CrudRepository<DataProfileDTO, String> {
}
Run Code Online (Sandbox Code Playgroud)

并包装它:

Flux.fromIterable(dataProfileRepository.findById(id)), DataProfileDTO.class))
Run Code Online (Sandbox Code Playgroud)