Spring Data REST
IntermediateExports repository operations as hypermedia-driven REST endpoints automatically, following HATEOAS principles with HAL or HAL-FORMS media types.
Overview
Spring Data REST (spring-boot-starter-data-rest) introspects your Spring Data repositories and automatically exposes CRUD operations as RESTful HTTP endpoints without requiring you to write controllers. Responses follow the HAL (Hypertext Application Language) format, embedding _links for navigation. The base path defaults to "/" and can be changed via spring.data.rest.base-path. You can control which repositories are exported with @RepositoryRestResource(exported=false), customise projections with @Projection, and intercept events with @RepositoryEventHandler. While excellent for rapid prototyping, production APIs typically need explicit controllers for business logic and fine-grained control.
Exposing a Repository
Extending any Spring Data repository interface is sufficient to get REST endpoints. Spring Data REST generates collection and item endpoints, and produces HAL+JSON responses with navigation links.
@RepositoryRestResource(collectionResourceRel = "products", path = "products")
public interface ProductRepository extends PagingAndSortingRepository<Product, Long> {
// Custom finder exposed at: GET /products/search/findByCategory?category=ELECTRONICS
List<Product> findByCategory(@Param("category") String category);
}
// Generated endpoints:
// GET /products — paginated list with _links
// POST /products — create
// GET /products/{id} — single item
// PUT /products/{id} — full replace
// PATCH /products/{id} — partial update
// DELETE /products/{id} — deleteProjections
@Projection defines a view interface that selects a subset of entity fields (or computed values). Clients request a projection by appending ?projection=name to the URL.
@Projection(name = "summary", types = { Product.class })
public interface ProductSummary {
String getName();
BigDecimal getPrice();
// Computed field — SpEL expression
@Value("#{target.category.name}")
String getCategoryName();
}
// Client request:
// GET /products/1?projection=summaryRepository Events & Validation
@RepositoryEventHandler intercepts lifecycle events (before/after create, save, delete) and is ideal for auditing or validation. Pair with a Validator bean for input validation.
@Component
@RepositoryEventHandler
public class ProductEventHandler {
@HandleBeforeCreate
public void handleBeforeCreate(Product product) {
if (product.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
throw new IllegalArgumentException("Price must be positive");
}
}
@HandleAfterSave
public void handleAfterSave(Product product) {
log.info("Product saved: {}", product.getId());
}
}Key Points to Remember
- 1Spring Data REST auto-generates CRUD + search endpoints from repository interfaces
- 2Responses are HAL+JSON with _links for navigation (self, collection, next/prev pages)
- 3@RepositoryRestResource(exported=false) hides a repository from the REST layer
- 4@Projection defines named field subsets; clients opt-in with ?projection=name
- 5@RepositoryEventHandler intercepts before/after save, create, delete events
- 6Use spring.data.rest.base-path=/api to namespace all generated endpoints
Interview Questions
Sign in to ask AriaWhat is HATEOAS and how does Spring Data REST implement it?
How do you prevent a repository from being exposed as a REST endpoint?
What is the difference between a Spring Data REST projection and a DTO?
When would you choose explicit @RestController over Spring Data REST?
How are custom finder methods exposed as search endpoints?
Ask Aria about Spring Data REST
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.