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!spring-doc.cn

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.spring-doc.cn

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:spring-doc.cn

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.spring-doc.cn

Consider the following class ,which uses Spring’s autowiring:spring-doc.cn

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:spring-doc.cn

Table 1. Ways to link to exported resources
Method Description

entityLinks.linkToCollectionResource(Person.class)spring-doc.cn

Provide a link to the collection resource of the specified type (Person, in this case).spring-doc.cn

entityLinks.linkToItemResource(Person.class, 1)spring-doc.cn

Provide a link to a single resource.spring-doc.cn

entityLinks.linkToPagedResource(Person.class, new PageRequest(…​))spring-doc.cn

Provide a link to a paged resource.spring-doc.cn

entityLinks.linksToSearchResources(Person.class)spring-doc.cn

Provides a list of links for all the finder methods exposed by the corresponding repository.spring-doc.cn

entityLinks.linkToSearchResource(Person.class, "findByLastName")spring-doc.cn

Provide a finder link by rel (that is, the name of the finder).spring-doc.cn

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.