4

I am calling ControllerLinkBuilder.linkTo method inside a spring Async method and it fails to find the current request.

service.setUrl(linkTo(Controller.class, Controller.METHOD_GET,
                headers.getFirst(HEADER_SOURCE), id).toString());

Exception:

java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Could not find current request via RequestContextHolder
Caused by: java.lang.IllegalStateException: Could not find current request via RequestContextHolder
Manan
  • 303
  • 1
  • 6

2 Answers2

0

Complementing the _Manan reponse, You can set the attribute inheritance to true on your properties file. Please add the following line:

spring.freemarker.request-context-attribute=true
whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46
-1

I tried to reconstruct your problem and weren't able to. Have you tried using something like the following?

linkTo(methodOn(EnvironmentRequestStateTransitioner.class)
    .approveRegistrationRequest(23L))
    .withRel("approve")

That worked fine for me using spring-boot-starter-parent version 1.3.2.RELEASE.

Dennis Stritzke
  • 5,198
  • 1
  • 19
  • 28
  • 1
    I had to make the `RequestAttributes` inheritable. `RequestAttributes currentRequestAttributes = RequestContextHolder.getRequestAttributes(); RequestContextHolder.setRequestAttributes(currentRequestAttributes, Boolean.TRUE);` – Manan Mar 02 '16 at 03:56
  • You had to do this to get it working or to produce the problem described in your question? – Dennis Stritzke Mar 02 '16 at 09:22
  • I had to do that to get it working. It basically exposes the `RequestAttributes` as inheritable for child threads. – Manan Mar 03 '16 at 02:35