Quick revision
The short answer
Spring Data REST turns repository interfaces into hypermedia REST resources in HAL JSON: a collection resource with paging and sorting, item resources, search resources for query methods and an ALPS profile. We control what is exported with @RepositoryRestResource and @RestResource(exported = false) or the annotated detection strategy, shape JSON with projections and excerpts, set base path and page size through spring.data.rest properties, and run validation and logic through repository event handlers and validators. It suits internal admin APIs and prototypes, because it exposes the persistence model directly.
What to remember
In this answer, we saw that Spring Data REST generates HAL resources from repositories: collections with paging and sorting, items, search resources for query methods and an ALPS profile. We control export with @RepositoryRestResource, @RestResource(exported = false) and the annotated detection strategy, shape output with projections and excerpts, configure paths and page sizes with spring.data.rest properties, and hook in logic and validation through repository events. It is best for internal and prototype APIs, because it couples clients to the persistence model.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring Data REST turns repository interfaces into hypermedia REST resources in HAL JSON: a collection resource with paging and sorting, item resources, search resources for query methods and an ALPS profile. We control what is exported with @RepositoryRestResource and @RestResource(exported = false) or the annotated detection strategy, shape JSON with projections and excerpts, set base path and page size through spring.data.rest properties, and run validation and logic through repository event handlers and validators. It suits internal admin APIs and prototypes, because it exposes the persistence model directly.
Why this question is asked
Writing CRUD controllers for every entity is repetitive: list with paging, get by id, create, update, delete, a few searches. Spring Data REST generates all of that from a repository interface. That is a big productivity gain, and also a risk, because it exposes everything unless we say otherwise. Interviewers ask about it to see whether we know what it generates and how to keep it under control. In this answer, we will see what it generates and what the JSON looks like, then how to control exports, shape the output, hook in validation and logic, and when it is the right tool.
1What Spring Data REST Generates
Spring Data REST sits on top of Spring Data repositories and Spring MVC. For each exported repository, it registers endpoints that call the repository's methods, and it formats the results as HAL, a JSON format for hypermedia in which each resource carries a _links object pointing to related resources. Take one repository.
Notice that the path people comes from the domain type Person, uncapitalised and pluralised. The ALPS profile is a machine-readable description of the resource's fields and operations, which clients and tools can read to discover the API.
2What the JSON Looks Like
A collection response wraps the items in _embedded, adds links to itself, the profile and the search resource, and includes page information. Each item carries its own self link instead of exposing the id as a separate field by default.
3Controlling What Is Exported
By default, every public repository is exported with all its CRUD and query methods. We can change the path or hide a repository with @RepositoryRestResource, and hide single methods with @RestResource(exported = false). For example, hiding deleteById and delete removes the DELETE endpoint. A safer default is to set spring.data.rest.detection-strategy=annotated, so that only repositories with @RepositoryRestResource are exported at all.
| Strategy | What gets exported |
|---|---|
| default | public repositories, unless marked exported = false |
| all | all repositories, including non-public ones |
| annotated | only repositories with @RepositoryRestResource |
| visibility | only public repositories |
Recent versions also offer an exposure configuration in RepositoryRestConfigurer, which disables HTTP methods per domain type in one place; check your version for which style is preferred. The detection strategy table below summarises the options.
4Shaping the JSON With Projections and Excerpts
By default, an item shows all entity fields that Jackson can serialise. A projection is an interface that names the fields to show, and can compute new ones. Clients request it with ?projection=summary. An excerpt is a projection used automatically when items are embedded in a collection, set with excerptProjection on the repository.
5Hooking In Validation and Logic
Spring Data REST publishes events around each write: before and after create, save, delete and link changes. A class annotated with @RepositoryEventHandler can handle them with methods annotated @HandleBeforeCreate, @HandleBeforeSave and so on, for example to set an audit field. Validators can be registered for the same events in a RepositoryRestConfigurer; a failed validation returns 400 with the errors.
So where should Spring Data REST be used? It is very good for internal admin tools and prototypes, where speed matters and the clients are ours. But it exposes the persistence model directly: entity field names become the API, and a schema change becomes an API change. For public or partner APIs, explicit controllers with DTOs keep the contract separate from the database.
6Follow-up questions
How would you add a custom endpoint next to generated ones?
Use @RepositoryRestController on a controller class. Its handler methods are mapped under Spring Data REST's base path and use the same HAL message converters, so a custom endpoint like POST /api/persons/1/merge sits naturally next to the generated ones. To add links to generated resources, pointing at the custom endpoint, register a RepresentationModelProcessor for EntityModel of the entity type.
Does Bean Validation on the entity still apply?
Yes, at the persistence level: Hibernate validates constraints such as @NotNull before writing, and the error surfaces as a constraint violation. Registering validators for the beforeCreate and beforeSave events gives cleaner 400 responses before the database is involved.
Can I use it with Spring Security?
Yes. Secure the URLs in the security filter chain and add method security such as @PreAuthorize on repository methods, because the generated endpoints call those methods. Test each exported endpoint and verb, since everything is exported unless restricted.
7Conclusion
In this answer, we saw that Spring Data REST generates HAL resources from repositories: collections with paging and sorting, items, search resources for query methods and an ALPS profile. We control export with @RepositoryRestResource, @RestResource(exported = false) and the annotated detection strategy, shape output with projections and excerpts, configure paths and page sizes with spring.data.rest properties, and hook in logic and validation through repository events. It is best for internal and prototype APIs, because it couples clients to the persistence model.
Open with
Spring Data REST generates a hypermedia REST API from Spring Data repositories. For a PersonRepository it creates a collection resource at /people with paging and sorting parameters, POST to create, item resources with GET, PUT, PATCH and DELETE, a search resource listing the query methods, so findByLastName becomes /people/search/findByLastName, and an ALPS profile under /profile.
Then, if they want more
Responses are HAL JSON, with _embedded for collections, _links on every resource and page information. To control it, I set spring.data.rest.detection-strategy to annotated so only repositories with @RepositoryRestResource are exported, use @RepositoryRestResource for the path or exported = false, and put @RestResource(exported = false) on methods such as deleteById to remove endpoints.
Base path and page sizes come from spring.data.rest properties. To shape the JSON, I use projections, interfaces naming the fields, requested with ?projection=, and an excerpt projection for embedded collection items. For logic, a @RepositoryEventHandler bean with @HandleBeforeCreate or @HandleBeforeSave, and validators registered for beforeCreate and beforeSave in a RepositoryRestConfigurer.
I would use it for internal admin APIs and prototypes, but not for public APIs, because it exposes the persistence model directly.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does Spring Data REST generate from a repository interface, and how do you control...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/64cc1b4928/