Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add links to Spring Data REST projections?

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 ?

like image 745
fortm Avatar asked Jan 25 '15 09:01

fortm


People also ask

What do spring data rest projections do?

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.

Why is Spring data rest not recommended in real world applications?

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.

What is @RepositoryRestController?

@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).

What is @RepositoryRestResource?

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.


1 Answers

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;
    }
}
like image 198
fortm Avatar answered Sep 19 '22 08:09

fortm