spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
917 stars 563 forks source link

How to add custom links to repositories #92

Open joelkuiper opened 11 years ago

joelkuiper commented 11 years ago

Hey all,

I'm trying to add a custom link to the repository URL but so far I've been unsuccessful.

Currently I have a custom query in my Repository (using the standard Spring data JPA method of extending single repositories) but now I'd like to expose that query method from within the repository with a HATEOAS link.

For example:

"links": [

    {
        "rel": "concept.search",
        "href": "http://localhost:8080/trialverse/concepts/search"
    },
   {
        "rel": "concept.treatments",
        "href": "http://localhost:8080/trialverse/concepts/treatments"
    }
]

I know you can add a ResourceProcessor<Resource<?>> to add links to a Resource, but is there a similar construct for the repositories themselves?

jhiemer commented 11 years ago

@joelkuiper I think your question is in correlation with my request: https://github.com/SpringSource/spring-data-rest/issues/85

May that be right?

joelkuiper commented 11 years ago

Ah yes! Didn't see that request, seems to be essentially the same.

On Apr 15, 2013, at 8:43 PM, Johannes Hiemer notifications@github.com wrote:

@joelkuiper I think your question is in correlation with my request: #85

May that be right?

— Reply to this email directly or view it on GitHub.

joelkuiper commented 11 years ago

@jhiemer Is it however currently possible to add a custom method to the search links which are exposed by default? I would assume that there would be a class I could overwrite or a ResourceProcessor I could add, but was unable to find a suitable one!

jhiemer commented 11 years ago

@joelkuiper me as well. The only way which is currently possible, as suggested by @olivergierke, is the creation of a Controller, which contains these specific custom methods. The only issue I have is, that I was not able to merge the mappings of SD REST and the manual mappings of Spring MVC.

Did you try that?

joelkuiper commented 11 years ago

@jhiemer I was looking for an angle to do that, but haven't been very successful so far unfortunately

jhiemer commented 11 years ago

@joelkuiper I tried several things, but they did not work out. So far we are forced to wait on a reply of @jbrisbin.

jbrisbin commented 11 years ago

The following ResourceProcessor implementation will match the root Resource:

@Bean
public ResourceProcessor<Resource<List>> rootLinksResourceProcessor() {
    return new ResourceProcessor<Resource<List>>() {
        @Override
        public Resource<List> process(Resource<List> resource) {
            resource.add(new Link("href", "rel"));
            return resource;
        }
    };
}

Notice that this is a naive approach. By that I mean I'm not interrogating the Resource to determine what kind of Links are being returned. This signature will match any Resource<List> being returned. That may not always be desired.

It would be possible for SDR to subclass Resource and return a specific type of resource for each return. I haven't thought through the side effects of that (if there are any) but I think that would be relatively low-risk.

jbrisbin commented 11 years ago

I added a special subclass for Resource that's returned on listing repositories. In the example submodule config you can see how to set up a ResourceProcessor that adds links.