I have created a Spring Data Rest projection (not an excerpt projection) and need to add some links to it only as these links do not hold significance with other projections of same entity nor with the entity itself.
How can we do this as far as I know using ResourceProcessor
I can add links to only entities, is it possible to add links for only that projection ?
Spring data REST Projection supports projecting only a selected fields from an entity representation. To do that, we can define those specific fields into our @Projection interface. Let's create a custom view of our Student entity with first name and last name fields.
It is not recommended in real-world applications as you are exposing your database entities directly as REST Services. While designing RESTful services, the two most important things that we consider are the domain model and the consumers. But, while using Spring Data REST, none of these parameters are considered.
@RepositoryRestResourceIf you want to write a custom handler for a specific resource taking advantage of Spring Data REST's settings, message converters, exception handling and more, you can use @RepositoryRestController (instead of the standard Spring MVC @Controller or @RestController annotations).
This is an interface that allows you to perform various operations with WebsiteUser objects. We also defined a custom query that will provide a list of users based on a given name. The @RepositoryRestResource annotation is optional and is used to customize the REST endpoint.
It seems it is possible just to create a ResourceProcessor
dedicated to a projection and I could create 3 ResourceProcessors
one for each projection and one for entity itself and they get called depending on which projection is mentioned in URL.
@Component
public class UserProjectionResourceProcessor
implements ResourceProcessor<Resource<UserProjection>> {
public static final String CANCEL_REL = "cancel";
@Autowired
private EntityLinks entityLinks;
@Override
public Resource<UserProjection> process(Resource<UserProjection> resource) {
UserProjection userProjection = resource.getContent();
resource.add(entityLinks.linkFor(User.class).withRel(CANCEL_REL));
return resource;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With