@RepositoryRestResource is a Spring Data REST annotation that customises how a Spring Data repository is exposed as a REST resource. With Spring Data REST on the classpath, every repository is published automatically; this annotation lets you change the URL path, the collection rel name, the excerpt projection, or switch the export off.
It is only meaningful together with the spring-boot-starter-data-rest dependency (or spring-data-rest-webmvc), and it is placed on the repository interface.
Add the starter and a repository, and Spring Data REST builds a hypermedia API (HAL by default) with no controller code:
For this repository Spring Data REST publishes /people (a pluralised, lower-cased form of the entity name) with GET, POST, PUT, PATCH and DELETE support, paging, sorting and links. By default every public repository interface is exported. @RepositoryRestResource controls that behaviour for one repository.
| Attribute | Purpose |
|---|---|
path | URL segment of the collection resource, for example /members |
collectionResourceRel | Link relation name used for the collection in HAL _embedded |
itemResourceRel | Link relation name for a single item |
exported | false hides the repository from the REST API |
excerptProjection | Projection used to render items inside collection responses |
collectionResourceDescription, itemResourceDescription | ALPS metadata descriptions |
Now the collection lives at /members and the JSON contains _embedded.members. A request looks like this:
If you only change path, update clients accordingly. The rel name is separate and is what HAL clients use for navigation.
Use exported = false to keep a repository private, for example one that holds internal audit data:
To hide only some operations, annotate the method with @RestResource(exported = false):
Query methods are exposed under /members/search, so the second method answers GET /members/search/by-last-name?name=Lovelace. Query method parameters need @Param unless you compile with -parameters, which Spring Boot's build plugins enable by default.
An excerpt projection trims what is embedded in collection responses, so lists stay small while the item URL still returns everything:
A client can also request a projection explicitly with ?projection=summary.
Data REST exposes your persistence model directly, so treat it carefully:
@PreAuthorize) on repository methods.@JsonIgnore, projections or exported = false.spring.data.rest.base-path=/api so the generated API is not at the site root.No. Spring Data REST exports repositories by default. The annotation customises the export or turns it off.
Set path on the annotation, for example @RepositoryRestResource(path = "members").
Use @RepositoryRestResource(exported = false) on the interface, or @RestResource(exported = false) on one method.
path is the URL segment, and collectionResourceRel is the link relation name that appears in the _embedded and _links parts of the response.
@RepositoryRestResource customises Spring Data REST for a single repository: URL path, relation names, excerpt projection and whether it is exported. Combine it with @RestResource, projections and Spring Security to expose a controlled, hypermedia REST API.