Is it possible to export REST resources with custom (spring data) repositories?
How does it work?
I cannot find any example. I also have not found any claim that it is impossible.
Is it possible to export REST resources with custom (spring data) repositories?
How does it work?
I cannot find any example. I also have not found any claim that it is impossible.
Spring data rest specifically detects and does not export custom implementations on repositories. See the reference to the codebase here and the reason why here.
If you want to expose a custom repository implementation, you will need to use a custom controller. Documentation for how to appropriately use custom controllers is slated for Spring Data Rest 2.4 .
We used these two methods and both work fine so far:
RepositoryFactoryBeanSupport
), build your own PersistentEntityInformation
and take care of CRUD ops manually for your custom data storage type.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)
.
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.
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
(Base interface), CustomRepositoryImpl
(Default implementation).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.
Summarizing, the answer is yes, possible by implementing own storage SPI, but not easy. Should first look for other solutions including: