来自 ResourceProcessor 的摘录投影和自定义链接 [英] Excerpt Projection and Custom Links from ResourceProcessor

查看:29
本文介绍了来自 ResourceProcessor 的摘录投影和自定义链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 spring-data-rest 2.4.1 将实体公开为休息资源.

I am using spring-data-rest 2.4.1 to expose a entity as a rest resource.

我还实现了一个 ResourceProcessor 来添加到资源的自定义链接

I also implemented a ResourceProcessor to add a custom link to the resource

@Component
public class MyEntityResourceProcessor implements ResourceProcessor<Resource<MyEntity>> {

    @Override
    public Resource<MyEntity> process(Resource<MyEntity> resource) {
        resource.add(linkTo(methodOn(CustomController.class).getFeatures(resource.getContent().getId())).withRel("customRel"));
        return resource;
    }
}

这适用于单项资源.但我也设置了一个 ExcerptProjection 减少了集合资源中显示的属性:

This works fine for the single item resource. But I also have setup a ExcerptProjection that reduces the attributes shown in the collection resource:

@Projection(name = "myExcerptProjection", types = MyEntity.class)
interface MyExcerptProjection {

    String getName();
    String getSlogan();

}

使用投影时,我的 MyEntityResourceProcessor 未使用且自定义链接丢失.

When the projection is used my MyEntityResourceProcessor is not used and the custom link is missing.

我可以通过为投影实现一个 ResourceProcessor 来引入链接,如下所示:

I can bring in the link by implementing a ResourceProcessor for the projection like so:

public class MyEntityProjectionResourceProcessor implements ResourceProcessor<Resource<MyExcerptProjection>>

但我想避免这种情况,因为:

But I would like to avoid this because:

  • 这是代码重复
  • 而且我在投影中缺少实体 ID,因此无法生成链接

有什么想法可以让一个 ResourceProcessor 也适用于我的 ExcerptProjection?

Any ideas how I can have the one ResourceProcessor to also apply to my ExcerptProjection?

推荐答案

我针对我的问题创建了一个 JIRA 问题.

I created a JIRA issue with my question.

请参阅 https://jira.spring.io/browse/DATAREST-713

答案是 spring data rest 不能将实体资源处理器用于投影,并且您还需要用于投影的资源处理器.

The answer is that spring data rest cannot use the entity resource processors for projections and that you need resource processors for your projection as well.

为了避免代码重复,一个选项是引入一个由投影和实体扩展/实现的接口.然后我们可以为这个接口实现一个资源处理器,它将应用于投影和实体.

To avoid the code duplication an option is to introduce an interface that both the projection and the entity extend/implement. Then we could implement a resource processor for this interface which would be applied to both - the projection and the entity.

这篇关于来自 ResourceProcessor 的摘录投影和自定义链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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