尝试将 spring 数据 mongo 存储库与 Stream 一起使用时出现异常

Exception when trying to use spring data mongo repository with Stream

我扩展了 MongoRepository 并想使用 Stream API。作为参考,我声明了一个查询方法:

public Stream<MyType> findByIdIn(Iterable<? extends String> myTypeIds);

但是,当启动我的 Spring 引导应用程序时,抛出一个 PropertyNotFoundException 说明:

No property id found for type Stream!

我的域类型派生自包含 MongoDB 对象的实际 ID 的超类型。有什么解决办法吗?

我正在使用 Spring Boot 1.2.0.RELEASE 和 Spring Data MongoDB 1.6.1.RELEASE.

在 Spring 数据 MongoDB 版本 1.7 中添加了对 Stream 作为 return 类型的支持,as per the release notes:

What’s new in Spring Data MongoDB 1.7

...

  • Allow Stream as query method return type

因此,您需要从 1.6.1.RELEASE 至少更新到 1.7 版。如果您使用的是 Maven,则可以为当前最新版本添加以下依赖项,即 1.8.2.RELEASE:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.8.2.RELEASE</version>
</dependency>