Excerpt Projection and Custom Links from ResourceProcessor

馋奶兔 提交于 2020-01-14 14:26:07

问题


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

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

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();

}

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

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:

  • it is code duplication
  • and i am missing the entity id in the projection so I cannot generate the link

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


回答1:


I created a JIRA issue with my question.

Plese see https://jira.spring.io/browse/DATAREST-713

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.



来源:https://stackoverflow.com/questions/33501648/excerpt-projection-and-custom-links-from-resourceprocessor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!