This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Data REST 4.4.0! |
Integration
This section details various ways to integrate with Spring Data REST components, whether from a Spring application that is using Spring Data REST or from other means.
Programmatic Links
Sometimes you need to add links to exported resources in your own custom-built Spring MVC controllers. There are three basic levels of linking available:
-
Manually assembling links.
-
Using Spring HATEOAS’s
LinkBuilder
withlinkTo()
,slash()
, and so on. -
Using Spring Data REST’s implementation of
RepositoryEntityLinks
.
The first suggestion is terrible and should be avoided at all costs. It makes your code brittle and high-risk. The second is handy when creating links to other hand-written Spring MVC controllers. The last one, which we explore in the rest of this section, is good for looking up resource links that are exported by Spring Data REST.
Consider the following class ,which uses Spring’s autowiring:
public class MyWebApp {
private RepositoryEntityLinks entityLinks;
@Autowired
public MyWebApp(RepositoryEntityLinks entityLinks) {
this.entityLinks = entityLinks;
}
}
With the class in the preceding example, you can use the following operations:
Method | Description |
---|---|
|
Provide a link to the collection resource of the specified type ( |
|
Provide a link to a single resource. |
|
Provide a link to a paged resource. |
|
Provides a list of links for all the finder methods exposed by the corresponding repository. |
|
Provide a finder link by |
All of the search-based links support extra parameters for paging and sorting. See RepositoryEntityLinks for the details. There is also linkFor(Class<?> type) , but that returns a Spring HATEOAS LinkBuilder , which returns you to the lower level API. Try to use the other ones first.
|