我有一个可以工作的芒果储存库
@Repository
interface NewsRepository : ReactiveMongoRepository<News, String> {
fun findByUserId(userId: String): Flux<News>
fun findByType(type: NewsType, pageable: Pageable): Flux<News>
fun countByType(type: NewsType): Long
}服务可以调用存储库的findByUserId和findByType方法,并返回一个新闻列表。
我一调用countByType,就会得到以下异常:
System.out.println(newsRepository.countByType(NewsType.OFFICIAL))19-10-28 14:44:25.131 DEBUG 2986 -- ctor-http-nio-4 o.s.d.m.r.query.MongoQueryCreator :已创建查询:{“类型”:{ "$java“:官方},字段:{ },排序:{} 2019-10-28 14:44:25.165 ERROR 2986 -- ctor- HTTP -nio-4 a.w.r.e.AbstractErrorWebExceptionHandler : a29dfc95 500 Server Error for HTTP“/api/user/news/page”
$Proxy171.CountByType(未知源) ~na:na ( sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法) ~na:1.8.0_151 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~na:1.8.0_151
我猜这和ReactiveMongoRepository签名<News, String>有关
countByxxx似乎是一件事(https://www.logicbig.com/tutorials/spring-framework/spring-data/derived-count-query.html),由于findByType工作,我很困惑.
发布于 2019-10-28 14:24:38
看起来,在ReactiveMongoRepository中,我不能有一个返回“非反应性”数据类型的方法。
因此,除了最初的NewsRepository之外,我还创建了一个类似于
@Repository
interface NewsAggregationRepository: CrudRepository<News, Long> {
fun countByType(type: NewsType): Long
}这次它扩展了CrudRepository
我可以沿着反应性的服务将这个存储库注入到我的服务中:
class NewsServiceImpl(private val newsRepository: NewsRepository, private val newsAggregationRepository: NewsAggregationRepository)从那里调用反应性和非反应性方法
sysout(newsAggregationRepository.countByType(xxx))
sysout(newsRepository.getNews())https://stackoverflow.com/questions/58592272
复制相似问题