问题
The question may sound funny but I think this should be possible.
What I want is to use a repository that is purely custom but is exposed just like a Repository. This service would have methods to get, save, delete and list objects where the data could be from any arbitrary source.
Looking through the code, I think it should be possible since methods are accessed using CrudMethods and RepositoryInvoker. I belief this requires an implementation of RepositoryFactoryInformation that will be discovered by Repositories. I started experimenting a bit and it looks like a full-blown spring-data-noop module.
Am I on the right track or is there an easier way to accomplish this?
回答1:
I've ended up writing spring-data-custom to create fully customized spring-data repositories, allowing custom code to be used with spring-data-rest etc.
- Enable custom repositories (
@EnableCustomRepositories) - Annotate eligible entities (
@Custom) - Create a repository (extend
CustomRepository<T, ID>) - Add custom behavior:
- Let repository extend a new interface with the
Customsuffix - Create an implementation of the new interface with the
Implprefix - Add one or more CRUD methods named
findOne,save,findAll,delete(seeDefaultCrudMethods) - Add query methods annotated with
@Query
- Let repository extend a new interface with the
- Export repository using
spring-data-rest
(copied from README)
As @wwadge correctly mentioned, spring-data-keyvalue is an alternative. Repositories have to implement KeyValueAdapter, e.g. MapKeyValueAdapter.
回答2:
The easier way is to use spring-data-keyvalue project which does what you are trying to do.
来源:https://stackoverflow.com/questions/35554297/is-it-possible-to-use-spring-data-rest-webmvc-without-an-actual-repository