Spring Data REST exposes your Spring Data repositories as a hypermedia-driven REST API with almost no controller code. Add spring-boot-starter-data-rest, declare a JPA repository interface, and Spring publishes collection and item endpoints with paging, sorting, links and the standard HTTP verbs. Responses use the HAL format by default.
It is best for CRUD-style resources where the API mirrors your data model. When the API needs to differ from your entities, or contain business workflows, use projections, event handlers or ordinary @RestController classes alongside it.
Add the starter next to Spring Data JPA and a database driver.
Spring Boot configures everything through auto-configuration. Optionally set a base path so the API does not collide with other controllers:
Define an entity and a repository. With Spring Boot 3 and later the persistence annotations come from jakarta.persistence.
In Spring Data 3, CrudRepository and PagingAndSortingRepository are separate interfaces, so extend both (or JpaRepository) to get CRUD and paging. Without @RepositoryRestResource, the path defaults to the pluralised, uncapitalised entity name.
| Method and path | Purpose |
|---|---|
GET /api/products | Paged collection |
POST /api/products | Create an item |
GET /api/products/{id} | Read one item |
PUT /api/products/{id} | Replace an item |
PATCH /api/products/{id} | Partially update an item |
DELETE /api/products/{id} | Delete an item |
GET /api/products/search | List of exposed query methods |
GET /api/products/search/findByCategory?category=books | Run a query method |
GET /api | Root with links to every resource |
Query methods appear only under /search, and they need @Param when parameter names are not compiled into the class. Example request and response:
Exposing a repository exposes every public method of it. Restrict that explicitly.
To hide a whole repository, annotate it with @RepositoryRestResource(exported = false). Never expose repositories for entities containing sensitive fields unless you hide those fields with @JsonIgnore or a projection, and protect the endpoints with Spring Security method or URL rules.
A projection is an interface that selects the fields returned. It stops you from leaking internal columns and lets clients ask for a smaller view.
Request it with GET /api/products?projection=summary. To make it the default for a repository, set excerptProjection on @RepositoryRestResource.
Spring Data REST bypasses controllers, so use event handlers for validation, auditing or defaults.
For anything more complex than a single-entity rule, write a custom @RepositoryRestController for that endpoint and keep Spring Data REST for the rest.
Set spring.data.rest.base-path=/api in your configuration. All repository resources then appear under that prefix.
They are only exposed under the /search resource, and the repository must be exported. Check GET /api/products/search to see the exposed methods.
Only with care. Secure the endpoints, hide sensitive fields, disable unneeded HTTP methods with @RestResource(exported = false), and validate input in event handlers.
Spring Data REST is built around hypermedia and HAL. If you need a different shape, write regular controllers or use projections.
Spring Data REST turns repositories into REST resources: add the starter, define a repository, and you get paged CRUD endpoints, search resources and HAL links. Use @RepositoryRestResource and @RestResource(exported = false) to control exposure, projections to shape output, event handlers for logic, and Spring Security to protect the API.