Skip to content

RepresentationModelProcessor processes CollectionModel of alien entities #1373

Open
@escudero380

Description

@escudero380

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?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions