将Spring Data的ReactiveCrudRepository应用于Redis

16

我正在使用Spring Boot 2和webflux进行开发。 我尝试使用ReactiveSortingRepository来简化redis操作。

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

只需使用此界面即可。

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

异常:

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]

出现了错误。

在调试模式下,我发现该存储库的行为与反应堆不一致。可以看到从Redis中获取了一个实际的DataProfileDTO,但在尝试时失败了:

GENERIC_CONVERSION_SERVICE.convert(reactiveObject, targetWrapperType);

ReactiveWrapperConverters.toWrapper

我搜索了一下,似乎Spring Data Redis 2.0没有提到响应式存储库支持。我在想是不是我的代码有问题,还是Spring Data Redis 2.0尚未支持ReactiveCrudRepository。


你成功解决了吗? - javabeats
Spring Data Redis 2.3.8.RELEASE不支持响应式存储库。在bean创建时,异常消息为“org.springframework.dao.InvalidDataAccessApiUsageException:Redis不支持响应式存储库。冒犯的存储库是xx.xxxx.xxxx.XRepository!” - Metin YAVUZ
1个回答

6
根据Spring的文档,使用Reactive Redis Support与Redis进行交互的最高级抽象是ReactiveRedisTemplate。而ReactiveRedisConnection是一个更低层次的抽象,处理二进制值(ByteBuffer)作为输入和输出。
没有提到对反应式存储库的支持。您还可以在spring-data github repo中查看官方反应式示例。
为了使所有这些工作正常运行,您需要在使用的驱动程序中具有反应式支持 - 目前是lettuce
虽然不是理想的解决方案,但另一种选择是使用Flux.fromIterable()。您可以使用阻塞存储库并以反应式方式处理结果。
public interface DataProfileRepository extends CrudRepository<DataProfileDTO, String> {
}

并将其包装:

Flux.fromIterable(dataProfileRepository.findById(id)), DataProfileDTO.class))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接