Spring Data Rest and custom repositories

眉间皱痕 提交于 2019-12-05 16:27:56
Jay

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:

  • Implement custom controllers to utilize your custom service layer
  • Implement a custom repository factory (e.g. extending from 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:

  • Metadata: CustomEntityInformation, CustomEntityMappingContext, CustomPersistentEntity, CustomPersistentProperty.
  • Integration: @EnableCustomRepositories, CustomRepositoriesRegistrar, CustomRepositoryConfigurationExtension, CustomRepositoryFactory, CustomRepositoryFactoryBean.
  • Implementation: 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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!