spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
920 stars 563 forks source link

Link discovery by looking at repository finders [DATAREST-600] #974

Open spring-projects-issues opened 9 years ago

spring-projects-issues commented 9 years ago

Oliver Trosien opened DATAREST-600 and commented

At the moment structural links of a resource get populated by looking at the properties of an entity. This is fine so far, but Spring Data REST could do better. If you have a repository finder that takes an entity as input, it could actually be rendered as structural link from this entity. Example:

entity Product 
entity CustomerRating

repository CustomerRatingRepository {
  Page<CustomerRating> findByProduct(@Param("product") Product p, Pageable pageable);
}

Of course I could add a @OneToMany from Product to CustomerRating, but let's assume this would break modularity of my system. Still it would be nice to have a link on the REST API from a product to its customer ratings. What do you think? Is this a valid use case? Is this possible (e.g. adding annotations to the finder)?


Issue Links:

1 votes, 4 watchers

spring-projects-issues commented 9 years ago

Oliver Trosien commented

Looking for feedback - is this something useful?

spring-projects-issues commented 9 years ago

Oliver Drotbohm commented

The question here is how we'd find out whether you'd want that link to appear or not. You can already easily add the link yourself by implementing a ResourceProcessor<Resource<Product>> in the rating subsystem and manually add the link here using a RepositoryEntityLinks.linkToSearchResource(CustomerRating.class, "findByProduct")

spring-projects-issues commented 9 years ago

Oliver Trosien commented

Indeed, this is true, a CustomerResourceProcessor can be used to add links, as implemented here:

https://github.com/otrosien/spring-data-examples/blob/backlinkToFInder/src/main/java/example/customer/CustomerResourceProcessor.java

It should probably use some dependency injection in order to collect all link builders. Even better would be adding an annotation to the finder and letting spring inject the finder links. That's basically the feature request here

spring-projects-issues commented 9 years ago

Oliver Drotbohm commented

Well, I suggested to autowire a RepositoryEntityLinks instance above. You can use this to create a link to the search resource and then expand it:

// Identify product
Link productLink = entityLink.linkToSingleResource(Product.class, product.getId());

// Lookup search resource, expand with pointer to product
Map<String, String> parameters = Collections.singletonMap("product", productLink.expand().getHref())
Link link = entityLink.linkToSearchResource(CustomerRating.class, "findByProduct").expand(parameters);