如何添加指向 Spring Data REST 投影的链接? [英] How to add links to Spring Data REST projections?

查看:43
本文介绍了如何添加指向 Spring Data REST 投影的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Spring Data Rest 投影(不是摘录投影),并且只需要向它添加一些链接,因为这些链接与同一实体的其他投影或实体本身无关.

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.

据我所知,我们如何使用 ResourceProcessor 我可以只为实体添加链接,是否可以只为该投影添加链接?

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 ?

推荐答案

似乎可以只创建一个专用于投影的 ResourceProcessor 并且我可以创建 3 个 ResourceProcessor> 每个投影一个,实体本身一个,根据 URL 中提到的投影调用它们.

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;
    }
}

这篇关于如何添加指向 Spring Data REST 投影的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆