spring-projects / spring-projects/spring-hateoas
RepresentationModelProcessor processes CollectionModel of alien entities
@odrotbohm is already working on this.
Since Oct 28, 2020.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 476
- PR merge metrics
- No merged PRs in 30d
Description
In my Spring Boot project, I have two entities like Seller and Buyer. By extending CrudRepository, I enable Spring Data REST to automatically create a CollectionModel for each of them accessible via corresponding endpoints like /sellers and /buyers. Now, I want to add a custom "filter"-link to the CollectionModel for Buyer by writing a RepresentationModelProcessor like this:
@Component
public class BuyerCollectionModelProcessor implements
RepresentationModelProcessor<CollectionModel<Buyer>> {
@Override
public CollectionModel<Buyer> process(CollectionModel<Buyer> model) {
// here we add a link to some filter() method from BuyersController class
return model.add(linkTo(methodOn(BuyersController.class)
.filterBy(Optional.empty(), Optional.empty()))
.withRel("filter"));
}
}
It works as expected with the /buyers endpoint which now includes "filter"-link:
"_links": {
"self": {
"href": "http://localhost:8080/buyers"
},
"filter": {
"href": "http://localhost:8080/buyers/filterBy{?city,age}",
"templated": true
}
}
But as a side-effect, the /sellers endpoint now also gets the buyers "filter"-link:
"_links": {
"self": {
"href": "http://localhost:8080/sellers"
},
"filter": {
"href": "http://localhost:8080/buyers/filterBy{?city,age}",
"templated": true
}
}
And if I decide to add a custom link to /sellers in a similar way, then /sellers and /buyers will have the two links each. How can I fix that and separate custom links, so that they are included only in the CollectionModel for their corresponding entities?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.