How to add links to Spring Data REST projections?
Asked Answered
S

1

9

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 ?

Saidee answered 25/1, 2015 at 9:59 Comment(0)
S
17

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;
    }
}
Saidee answered 25/1, 2015 at 13:56 Comment(3)
userProjection is never used inside the process method. Is this a matter of demonstrating the resource.getContent() method?Janus
Mister Oliver, can you help us with above question? Tks!Furious
@Janus I think you are right. UserProjection is used for demonstration purposes becuse u cannot add a Link to a projection interface. You can only add Links to resourcesVasilikivasilis

© 2022 - 2024 — McMap. All rights reserved.