【发布时间】:2022-01-18 14:57:45
【问题描述】:
我想使用 ReactiveCrudRepository 创建一个响应式存储库
我试过这段代码:
import io.nexo.admin.console.domain.ActivePairs;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
@Repository
public interface ActivePairsSearchRepository extends ReactiveCrudRepository<ActivePairs, Long> {
Flux<ActivePairs> findAll(Specification spec, Pageable pageable);
}
但在部署过程中出现错误:
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract reactor.core.publisher.Flux io.nexo.admin.console.repository.ActivePairsSearchRepository.findAll(org.springframework.data.jpa.domain.Specification,org.springframework.data.domain.Pageable)! No property findAll found for type Pairs
你知道我该如何解决这个问题吗?
【问题讨论】:
-
JPA 无法响应,因为 JPA 在设计上是阻塞的。所以这根本行不通。
-
有哪些替代解决方案?
-
不使用 JPA 反应式。尽管如此,它仍然是阻塞的,因此试图将其硬塞到反应堆栈中只会使事情复杂化。但是你总是可以只返回一个列表(只需使用常规的
findAll并将List或Stream转换为Flux,仍然阻塞,因为所有结果都被获取而不是以反应的方式)。跨度> -
你能给我看一下代码示例吗,请问如何进行转换?
-
只需使用常规的
JpaRepostiry并在调用代码中使用Flux工厂方法转换findAll调用的结果。
标签: java spring spring-boot spring-data-jpa spring-webflux