spring-projects / spring-projects/spring-data-rest

Use Entity ResourceProcessor when Serializing Projection [DATAREST-713]

Open
#1,082 13 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

type: enhancement
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Mathias D opened DATAREST-713 and commented

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 invoked 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

By default I would expect spring data rest to use the ResourceProcessor of the Entity the Projection belongs to


Affects: 2.4.1 (Gosling SR1)

Reference URL: http://stackoverflow.com/questions/33501648/excerpt-projection-and-custom-links-from-resourceprocessor

1 votes, 4 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.