【发布时间】:2018-02-11 18:14:19
【问题描述】:
我有以下问题。我制作了一个使用 spring-data 并使用 spring-data-rest 将其公开为 REST 服务的应用程序。一切都很顺利,直到我想要一个自定义实现。我使用一种附加方法创建了 CustomSomethingRepository 和 SomethingRepositoryImpl。 Spring数据存储库接口扩展了CustomSomethingRepository,一切都很好,我可以直接从测试中执行我的方法,自定义实现也被执行了。然后我尝试通过 REST api 获取它,在这里我很惊讶这种方法不能通过 /somethings/search 获得。我几乎百分百确定它在 spring boot 1.3.x 和 JpaRepositories 中运行良好。现在我正在使用 boot 1.5.x 和 MongoRepository。请看我的示例代码:
@RepositoryRestResource
public interface SomethingRepository extends CrudRepository<Something>, CustomSomethingRepository {
//this one is available in /search
@RestResource(exported = true)
List<Something> findByEmail(String email);
}
自定义界面
public interface CustomSomethingRepository {
//this one will not be available in /search which is my problem :(
List<Something> findBySomethingWhichIsNotAnAttribute();
}
和实施
@RepositoryRestResource
public class SomethingRepositoryImpl implements CustomSomethingRepository {
@Override
public List<Something> findBySomethingWhichIsNotAnAttribute() {
return new ArrayList<>(); //dummy code
}
}
您能否给我一个提示,我如何将 CustomSomethingImpl 公开为 Rest 端点的一部分,而无需创建另一个仅处理单个请求的常规 spring mvc bean?
我读过这样的问题:Implementing custom methods of Spring Data repository and exposing them through REST 声明这是不可能实现的,但信不信由你,我有一个带有 spring-boot 1.3.x 的项目,并且这些实现也被公开了 :) .
谢谢!
【问题讨论】:
-
也许我的howto 会有所帮助..
-
您好,谢谢您的回答,但它没有公开自定义实现,您正在创建另一个控制器,这是一种解决方法,我想说。问题是如何自动公开自定义实现,这可以从 Spring 数据级别获得。
-
我的注意力不集中..))
-
由于您的自定义方法返回一个 List
您应该将它放在Something Repository 中,spring 数据将把它放在/search 路径上。添加列表 findByNotAttribute()
标签: java spring rest spring-data spring-data-rest