Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the default exposure of Spring Data REST repositories?

I have a project that uses spring-data-rest, and has a dependency project that only uses Spring Data. Both projects have spring data repositories and use @EnableJpaRepositories to implement their repository interfaces, but I only want to export the repositories in the parent project.

Here's my question: is there some way to configure Spring Data REST to only expose rest endpoints for resources in the parent project, without having to explicitly annotate every repository in the dependency project with @RepositoryRestResource(exported = false)?

If I can only do this with @RepositoryRestResource of disabling it, and worse yet, no other project with a different use case will be able to enable REST endpoints for those repositories, my dependency project will have to include Spring Data REST solely for the…

like image 288
gyoder Avatar asked Feb 04 '15 20:02

gyoder


People also ask

Is used for exposing spring data repositories over rest using Spring data rest?

Spring Data REST can be used to expose HATEOAS RESTful resources around Spring Data repositories. Without writing a lot of code, we can expose RESTful API around Spring Data Repositories.

What does the @RepositoryRestResource annotation do?

@RepositoryRestResource is used to set options on the public Repository interface - it will automatically create endpoints as appropriate based on the type of Repository that is being extended (i.e. CrudRepository/PagingAndSortingRepository/etc).

Why is Spring data rest not recommended in real world applications?

Real-world applications should avoid using Spring Data REST because the entities are exposed as RESTful Services. The two most critical considerations in designing a RESTful service are the domain model and the consumers.

What is rest repository in spring boot?

This repository is an interface that lets you perform various operations involving Person objects. It gets these operations by extending the PagingAndSortingRepository interface that is defined in Spring Data Commons. At runtime, Spring Data REST automatically creates an implementation of this interface.


2 Answers

Looping back here as I was looking for this specific setting. It looks like this is now implemented. In this case, you would want to set spring.data.rest.detection-strategy=annotated to avoid default exposure.

All application.properties options:

# Exposes all public repository interfaces but considers @(Repository)RestResource\u2019s `exported flag. spring.data.rest.detection-strategy=default  # Exposes all repositories independently of type visibility and annotations. spring.data.rest.detection-strategy=all  # Only repositories annotated with @(Repository)RestResource are exposed, unless their exported flag is set to false. spring.data.rest.detection-strategy=annotated  # Only public repositories annotated are exposed. spring.data.rest.detection-strategy=visibility 

References

  • 3.5.1. Setting the Repository Detection Strategy
  • Common Application Properties
like image 55
Brian Avatar answered Oct 02 '22 20:10

Brian


Currently there's no global switch for what you're looking for. I've filed this ticket for you for inclusion in the next major release.

Not sure if it is an option for you but package private repository interfaces are not currently exposed unless explicitly annotated. If you can make all those library repositories package protected that might be favorable over the explicit annotation.

like image 26
Oliver Drotbohm Avatar answered Oct 02 '22 21:10

Oliver Drotbohm