7

Using spring data REST I have exposed a ProjectRepository that supports listing projects and performing CRUD operations on them. When I go to http://localhost:8080/projects/ I get the list of projects as I expect.

What I am trying to do is add a custom action to the _links section of the JSON response for the Project Collection.

For example, I'd like the call to http://localhost:8080/projects/ to return something like this:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/projects/{?page,size,sort}",
      "templated" : true
    },
    "search" : {
      "href" : "http://localhost:8080/projects/search"
    },
    "customAction" : {
       "href" : "http://localhost:8080/projects/customAction"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 0,
    "totalPages" : 0,
    "number" : 0
  }
}

Where customAction is defined in some controller.

I've tried creating the following class:

public class ProjectCollectionResourceProcessor implements ResourceProcessor<Resource<Collection<Project>>> {

    @Override
    public Resource<Collection<Project>> process(Resource<Collection<Project>> listResource) {
        // code to add the links to customAction here
        return listResource;
    }

}

and adding adding the following Bean to my applications configuration:

@Bean
public ProjectCollectionResourceProcessor projectCollectionResourceProcessor() {
    return new ProjectCollectionResourceProcessor();
}

But process(...) doesn't ever seem to get called. What is the correct way to add links to Collections of resources?

2 Answers2

4

The collection resources render an instance of Resources<Resource<Project>>, not Resource<Collection<Project>>. So if you change the generic typing in your ResourceProcessor implementation accordingly that should work as you expect it.

Sam
  • 1,832
  • 19
  • 35
Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • adding below code didn't help `public class ProjectsResourceProcessor implements ResourceProcessor> { @Autowired private EntityLinks entityLinks; @Override public Resources process(Resources resources) { resources.add(entityLinks.linkFor(Project.class).slash("custom") .withRel("custom")); return resources; } }` Using SDR-2.1.0.RELEASE – Stackee007 Jun 18 '14 at 15:58
  • In order for me to get it to work I had to implement `ResourceProcessor` and within the code determine if it was a `Project` resource. Thanks for the help. – thorben.jakobsen Jun 19 '14 at 12:20
  • @thorben.jakobsen I'm looking for some resources (learning ones :-) to implement your solution. I have a paged resource as well. – Stephane Aug 18 '14 at 21:16
  • @thorben.jakobsen I wonder how your search end point link would look if it offered pagination. – Stephane Aug 18 '14 at 21:16
  • 1
    @thorben.jakobsen How did you actually manage to do the type checking? Do you have a generic way to share with otherS? – RJo Oct 07 '14 at 05:54
  • This doesn't work, ResourceProcessor> will not be invoked for my paged aggregate root of /users – szxnyc Jan 12 '18 at 19:52
4

I had the same issue. What worked for me was:

public class ProjectsResourceProcessor implements ResourceProcessor<PagedResources<Resource<Project>>> {

    private final @NonNull EntityLinks entityLinks;

    @Override
    public PagedResources<Resource<Project>> process(PagedResources<Resource<Project>> pagedResources) {

       ...

        return pagedResources;
    }
}
Kakawait
  • 3,929
  • 6
  • 33
  • 60