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?

4

2 回答 2

4

集合资源呈现 的实例Resources<Resource<Project>>,而不是Resource<Collection<Project>>。因此,如果您相应地更改实现中的通用类型ResourceProcessor,则应该可以按预期工作。

于 2014-06-18T14:21:31.647 回答
4

我遇到过同样的问题。对我有用的是:

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;
    }
}
于 2014-11-06T15:51:02.867 回答