4

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.

user1648825
  • 979
  • 11
  • 19

2 Answers2

3

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 .

Community
  • 1
  • 1
Jason
  • 7,356
  • 4
  • 41
  • 48
3

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:

aux
  • 1,589
  • 12
  • 20
  • Can you elaborate or link to documentation for this please? Particularly the second one. – afaulconbridge Apr 06 '17 at 16:00
  • Added explanation and relevant links. – aux Apr 07 '17 at 22:50
  • As a side note, we implemented this solution some time ago but now it looks like too much work... )) We used it with spring-data-rest to implement some sort of "remote resource" requested via REST from another micro-service but now we came to much simpler solution - using API gateway, projections and resource processors for custom links between services. – aux Apr 07 '17 at 23:14
  • Many thanks. We're looking into either using Spring-data-rest over a cross-store repository of some kind, or manually building the API ourselves. A short list of where to start is very helpful! – afaulconbridge Apr 09 '17 at 18:07