Spring Data Rest 和自定义存储库 [英] Spring Data Rest and custom repositories
问题描述
是否可以使用自定义(spring 数据)存储库导出 REST 资源?
Is it possible to export REST resources with custom (spring data) repositories?
它是如何工作的?
我找不到任何例子.我也没有发现任何声称这是不可能的.
I cannot find any example. I also have not found any claim that it is impossible.
推荐答案
我们使用了这两种方法,目前都运行良好:
We used these two methods and both work fine so far:
- 实现自定义控制器以利用您的自定义服务层
- 实现自定义存储库工厂(例如从
RepositoryFactoryBeanSupport
扩展),构建您自己的PersistentEntityInformation
并手动处理自定义数据存储类型的 CRUD 操作.
- Implement custom controllers to utilize your custom service layer
- Implement a custom repository factory (e.g. extending from
RepositoryFactoryBeanSupport
), build your ownPersistentEntityInformation
and take care of CRUD ops manually for your custom data storage type.
更新:查看文档的这一章:向所有存储库添加自定义行为.这个想法是使用 @EnableJpaRepositories(repositoryBaseClass = MyRepositoryImpl.class)
用您自己的实现替换默认的特定于存储的实现.
UPDATE: Look into this chapter of the documentation: Adding custom behavior to all repositories. The idea is to replace the default storage-specific implementation with your own using @EnableJpaRepositories(repositoryBaseClass = MyRepositoryImpl.class)
.
如果您想构建自定义存储 SPI,那就是另一回事了.您可以使用 spring-data-keyvalue 并实现您自己的 您为
bean.查看 spring-data-redis 源代码作为该实现的示例.这是最简单的解决方案.@EnableMapRepositories
指定的 KeyValueOperations
If you want to build a custom storage SPI that's a different story. You can use a spring-data-keyvalue and implement your own KeyValueOperations
bean that you specify for @EnableMapRepositories
. Look into spring-data-redis source as an example of that implementation. That's the easiest solution.
从头开始为自己的存储库构建完整的 SPI 需要更多代码.我们遵循了 spring-data-elasticsearch 的来源.您可能需要实施:
Building a complete SPI for own repositories from scratch needs more code. We followed sources from spring-data-elasticsearch. You might need to implement:
- 元数据:
CustomEntityInformation
、CustomEntityMappingContext
、CustomPersistentEntity
、CustomPersistentProperty
.立> - 集成:
@EnableCustomRepositories
、CustomRepositoriesRegistrar
、CustomRepositoryConfigurationExtension
、CustomRepositoryFactory
、<代码>CustomRepositoryFactoryBean. - 实现:
CustomRepository
(基础接口),CustomRepositoryImpl
(默认实现).
- Metadata:
CustomEntityInformation
,CustomEntityMappingContext
,CustomPersistentEntity
,CustomPersistentProperty
. - Integration:
@EnableCustomRepositories
,CustomRepositoriesRegistrar
,CustomRepositoryConfigurationExtension
,CustomRepositoryFactory
,CustomRepositoryFactoryBean
. - Implementation:
CustomRepository
(Base interface),CustomRepositoryImpl
(Default implementation).
而且还需要一些额外的代码来支持spring-data-rest,比如搜索资源不会自动暴露,所以我们手动构建搜索资源.然后你可能想在顶部添加查询支持,等等.
And still needs some extra code for spring-data-rest support, for example, search resources are not exposed automatically, so we build search resources manually. Then you might want to add queries support on top, etc.
总而言之,答案是肯定的,可以通过实现自己的存储 SPI 来实现,但并不容易.应首先寻找其他解决方案,包括:
Summarizing, the answer is yes, possible by implementing own storage SPI, but not easy. Should first look for other solutions including:
- 预测
- 资源处理器
- 自定义控制器
- 其他 spring data rest定制 可能性
这篇关于Spring Data Rest 和自定义存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!