Skip to content

ControllerLinkBuilder to create templated links #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
matthewdfleming opened this issue Aug 23, 2014 · 8 comments
Closed

ControllerLinkBuilder to create templated links #234

matthewdfleming opened this issue Aug 23, 2014 · 8 comments
Assignees

Comments

@matthewdfleming
Copy link

So my issue is that I would like to use the ControllerLinkBuilder to generate rfc6570 links. Right now it works fine with parameter substitution when I know the parameter values..

I'd love to be able to do something like this:

Link link = linkTo(methodOn(ClientsResource.class).get(DUMMY)).withRel("clientById")

To point to the method:

    @RequestMapping(value = "/clients/{id}", method = RequestMethod.GET)
    public ResponseEntity<ClientResource> get(@PathVariable("id") Long id)

I can obviously do that when I have a true parameter for the ID but I'd like to be able to publish a link to my application client in a templated way so that the link comes back as:

http://localhost:8080/whatever/clients{/id}.

This would also help me on 'search' kinds of links where the parameters are variable.

@matthewdfleming
Copy link
Author

Here's the best that I could come up with munging together different solutions..

    public Link createClientByIdLink() {
        DummyInvocationUtils.LastInvocationAware invocations = (DummyInvocationUtils.LastInvocationAware) methodOn(ClientsResource.class).get(1l);
        Method method = invocations.getLastInvocation().getMethod();
        Link link = linkTo(invocations).withRel("clientById");
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(link.getHref());
        return new Link(
                builder.replacePath(discoverer.getMapping(ClientsResource.class, method)).build().toUriString(),
                link.getRel());
    }

    private static final MappingDiscoverer discoverer = new AnnotationMappingDiscoverer(RequestMapping.class);

I'd love it if you had something better though..

@fdw
Copy link

fdw commented Feb 16, 2015

Has there been any progress on this issue? I'd like to use templated links as well.

@xenji
Copy link

xenji commented Oct 9, 2015

Is this the same as #169 ?

@matthewdfleming
Copy link
Author

At a high level it's the same kind of problem. The difference between the two is that this issue isn't about pagination or PagedResources.. I need this kind of link building for any parameters.

@xenji
Copy link

xenji commented Oct 9, 2015

Ok, from that perspective I agree. Btw. I need it also for any kind of params. As a quick fix, I will try to "copy" the current builder and change it to behave like I want. But this is ugly and I really like to get rid of that technical dept.

@olivergierke If you can point out some kind of implementation idea, I am willing to write the code. I am just not deep enough into the source (and sadly no spare time left to do so) to create this on my own.

Otherwise, I could push over my "hacked" version of the builder, if I get it working. Maybe this could be a start.

@matthewdfleming
Copy link
Author

Yeah on our project, we're pretty much doing what I outlined above (and have polluted our Resource/ResourceBuilder classes)..

I think the existing stack is useful for making links from a known entity to another.. like when I have a ParentResource making a link to a collection of ChildResource objects. The problem I have is how to get the link/rel to the collection of ParentResource objects that have no parent?

I'm not sure how hateoas solves this problem in general, but the 'hack' we're using allows us to make templated links that return objects that have no Parent context. E.g. when I have a front-end url like /parent/1 and I want to view Parent #1 without the 'context' of a search happening.

@fdw
Copy link

fdw commented Oct 25, 2016

Is there anything planned for this feature? I'd say it's one of the most basic ideas for HATEOAS, but the support could be better.

@odrotbohm
Copy link
Member

Resolving as a duplicate of #169.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants