Skip to content
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

ControllerLinkBuilder.linkTo throws error if called outside of the context of a HTTP request #408

Closed
nickgrealy opened this issue Oct 25, 2015 · 1 comment
Assignees
Milestone

Comments

@nickgrealy
Copy link

Current Behaviour

Given the latest spring hateos dependency org.springframework.hateoas:spring-hateoas:0.19.0.RELEASE,

When I attempt to use the ControllerLinkBuilder.linkTo method (outside of the context of a HTTP request),

String url = ControllerLinkBuilder.linkTo(
        methodOn(GreetingController.class).greeting("Nick"))
        .toUri().toString();

Then I get the following exception:

Exception in thread "main" java.lang.IllegalStateException: Could not find current request via RequestContextHolder
    at org.springframework.util.Assert.state(Assert.java:385)
    at org.springframework.hateoas.mvc.ControllerLinkBuilder.getCurrentRequest(ControllerLinkBuilder.java:234)
    at org.springframework.hateoas.mvc.ControllerLinkBuilder.getBuilder(ControllerLinkBuilder.java:186)
    at org.springframework.hateoas.mvc.ControllerLinkBuilderFactory.linkTo(ControllerLinkBuilderFactory.java:117)
    at org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo(ControllerLinkBuilder.java:135)

Proposed Behaviour

Instead of the exception, I'd expect to receive a relative URL (preferably including the context-path).

e.g. Instead of http://localhost:8080/myappscontext/mycontroller/mymethod/pathvar/foo?a=1, I'd receive /myappscontext/mycontroller/mymethod/pathvar/foo?a=1

Additionally, it'd be excellent if I could specify my own UriComponentsBuilder to be used for building the URI.

e.g.

String url = ControllerLinkBuilder.linkTo(UriComponentsBuilder.
        fromUriString("http://myproxy.com.au/"), 
        methodOn(GreetingController.class).greeting("Nick"))
        .toUri().toString();

Related

The solution proposed should (at least partially) also address these similar issues:

nickgrealy added a commit to nickgrealy/spring-hateoas that referenced this issue Oct 26, 2015
Changed linkTo so that it can be invoked outside of a HttpRequest -
issue spring-projects#408.
Added linkTo method, which takes an explicit UriComponentBuilder.
@mahmoudimus
Copy link

👍 super useful

gregturn added a commit that referenced this issue May 25, 2017
If forming a link outside a Spring web request, fallback to relative links.

Original pull-request: #410
Related issues: #330, #143, #516
gregturn added a commit that referenced this issue May 25, 2017
If forming a link outside a Spring web request, fallback to relative links.

Original pull-request: #410
Related issues: #330, #143, #516
gregturn added a commit that referenced this issue May 25, 2017
If forming a link outside a Spring web request, fallback to relative links.

Original pull-request: #410
Related issues: #330, #143, #516
@gregturn gregturn self-assigned this May 25, 2017
gregturn added a commit that referenced this issue May 25, 2017
If forming a link outside a Spring web request, fallback to relative links.

Original pull-request: #410
Related issues: #330, #143, #516
odrotbohm pushed a commit that referenced this issue Jul 27, 2017
If forming a link outside a Spring web request, fallback to relative links.

Original pull-request: #410.
Related issues: #330, #143, #516.
odrotbohm added a commit that referenced this issue Jul 27, 2017
Original pull-request: #410.
Related issues: #330, #143, #516.
@odrotbohm odrotbohm added this to the 0.24 milestone Jul 27, 2017
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